s3-spoolss: Move the standard mappings to spoolss.
[Samba/vl.git] / source3 / rpcclient / cmd_netlogon.c
blobfaa2bfff3efaf35cec28193d4a159659d06cc2d8
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"
26 #include "rpc_client/cli_netlogon.h"
28 static WERROR cmd_netlogon_logon_ctrl2(struct rpc_pipe_client *cli,
29 TALLOC_CTX *mem_ctx, int argc,
30 const char **argv)
32 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
33 WERROR werr;
34 const char *logon_server = cli->desthost;
35 enum netr_LogonControlCode function_code = NETLOGON_CONTROL_REDISCOVER;
36 uint32_t level = 1;
37 union netr_CONTROL_DATA_INFORMATION data;
38 union netr_CONTROL_QUERY_INFORMATION query;
39 const char *domain = lp_workgroup();
41 if (argc > 5) {
42 fprintf(stderr, "Usage: %s <logon_server> <function_code> "
43 "<level> <domain>\n", argv[0]);
44 return WERR_OK;
47 if (argc >= 2) {
48 logon_server = argv[1];
51 if (argc >= 3) {
52 function_code = atoi(argv[2]);
55 if (argc >= 4) {
56 level = atoi(argv[3]);
59 if (argc >= 5) {
60 domain = argv[4];
63 switch (function_code) {
64 case NETLOGON_CONTROL_REDISCOVER:
65 case NETLOGON_CONTROL_TC_QUERY:
66 data.domain = domain;
67 break;
68 default:
69 break;
72 status = rpccli_netr_LogonControl2(cli, mem_ctx,
73 logon_server,
74 function_code,
75 level,
76 &data,
77 &query,
78 &werr);
79 if (!NT_STATUS_IS_OK(status)) {
80 return ntstatus_to_werror(status);
83 if (!W_ERROR_IS_OK(werr)) {
84 return werr;
87 /* Display results */
89 return werr;
92 static WERROR cmd_netlogon_getanydcname(struct rpc_pipe_client *cli,
93 TALLOC_CTX *mem_ctx, int argc,
94 const char **argv)
96 const char *dcname = NULL;
97 WERROR werr;
98 NTSTATUS status;
99 int old_timeout;
101 if (argc != 2) {
102 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
103 return WERR_OK;
106 /* Make sure to wait for our DC's reply */
107 old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
108 rpccli_set_timeout(cli, MAX(old_timeout, 30000)); /* At least 30 sec */
110 status = rpccli_netr_GetAnyDCName(cli, mem_ctx,
111 cli->desthost,
112 argv[1],
113 &dcname,
114 &werr);
115 rpccli_set_timeout(cli, old_timeout);
117 if (!NT_STATUS_IS_OK(status)) {
118 return ntstatus_to_werror(status);
121 if (!W_ERROR_IS_OK(werr)) {
122 return werr;
125 /* Display results */
127 printf("%s\n", dcname);
129 return werr;
132 static WERROR cmd_netlogon_getdcname(struct rpc_pipe_client *cli,
133 TALLOC_CTX *mem_ctx, int argc,
134 const char **argv)
136 const char *dcname = NULL;
137 NTSTATUS status;
138 WERROR werr;
139 int old_timeout;
141 if (argc != 2) {
142 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
143 return WERR_OK;
146 /* Make sure to wait for our DC's reply */
147 old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
148 rpccli_set_timeout(cli, MAX(30000, old_timeout)); /* At least 30 sec */
150 status = rpccli_netr_GetDcName(cli, mem_ctx,
151 cli->desthost,
152 argv[1],
153 &dcname,
154 &werr);
155 rpccli_set_timeout(cli, old_timeout);
157 if (!NT_STATUS_IS_OK(status)) {
158 return ntstatus_to_werror(status);
161 if (!W_ERROR_IS_OK(werr)) {
162 return werr;
165 /* Display results */
167 printf("%s\n", dcname);
169 return werr;
172 static WERROR cmd_netlogon_dsr_getdcname(struct rpc_pipe_client *cli,
173 TALLOC_CTX *mem_ctx, int argc,
174 const char **argv)
176 NTSTATUS result;
177 WERROR werr = WERR_OK;
178 uint32 flags = DS_RETURN_DNS_NAME;
179 const char *server_name = cli->desthost;
180 const char *domain_name;
181 struct GUID domain_guid = GUID_zero();
182 struct GUID site_guid = GUID_zero();
183 struct netr_DsRGetDCNameInfo *info = NULL;
185 if (argc < 2) {
186 fprintf(stderr, "Usage: %s [domain_name] [domain_guid] "
187 "[site_guid] [flags]\n", argv[0]);
188 return WERR_OK;
191 if (argc >= 2)
192 domain_name = argv[1];
194 if (argc >= 3) {
195 if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
196 return WERR_NOMEM;
200 if (argc >= 4) {
201 if (!NT_STATUS_IS_OK(GUID_from_string(argv[3], &site_guid))) {
202 return WERR_NOMEM;
206 if (argc >= 5)
207 sscanf(argv[4], "%x", &flags);
209 debug_dsdcinfo_flags(1,flags);
211 result = rpccli_netr_DsRGetDCName(cli, mem_ctx,
212 server_name,
213 domain_name,
214 &domain_guid,
215 &site_guid,
216 flags,
217 &info,
218 &werr);
220 if (W_ERROR_IS_OK(werr)) {
221 d_printf("DsGetDcName gave: %s\n",
222 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
223 return WERR_OK;
226 printf("rpccli_netlogon_dsr_getdcname returned %s\n",
227 win_errstr(werr));
229 return werr;
232 static WERROR cmd_netlogon_dsr_getdcnameex(struct rpc_pipe_client *cli,
233 TALLOC_CTX *mem_ctx, int argc,
234 const char **argv)
236 WERROR result;
237 NTSTATUS status;
238 uint32_t flags = DS_RETURN_DNS_NAME;
239 const char *server_name = cli->desthost;
240 const char *domain_name;
241 const char *site_name = NULL;
242 struct GUID domain_guid = GUID_zero();
243 struct netr_DsRGetDCNameInfo *info = NULL;
245 if (argc < 2) {
246 fprintf(stderr, "Usage: %s [domain_name] [domain_guid] "
247 "[site_name] [flags]\n", argv[0]);
248 return WERR_OK;
251 domain_name = argv[1];
253 if (argc >= 3) {
254 if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
255 return WERR_NOMEM;
259 if (argc >= 4) {
260 site_name = argv[3];
263 if (argc >= 5) {
264 sscanf(argv[4], "%x", &flags);
267 debug_dsdcinfo_flags(1,flags);
269 status = rpccli_netr_DsRGetDCNameEx(cli, mem_ctx,
270 server_name,
271 domain_name,
272 &domain_guid,
273 site_name,
274 flags,
275 &info,
276 &result);
277 if (!NT_STATUS_IS_OK(status)) {
278 return ntstatus_to_werror(status);
281 if (!W_ERROR_IS_OK(result)) {
282 return result;
285 d_printf("DsRGetDCNameEx gave %s\n",
286 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
288 return result;
291 static WERROR cmd_netlogon_dsr_getdcnameex2(struct rpc_pipe_client *cli,
292 TALLOC_CTX *mem_ctx, int argc,
293 const char **argv)
295 WERROR result;
296 NTSTATUS status;
297 uint32_t flags = DS_RETURN_DNS_NAME;
298 const char *server_name = cli->desthost;
299 const char *domain_name = NULL;
300 const char *client_account = NULL;
301 uint32_t mask = 0;
302 const char *site_name = NULL;
303 struct GUID domain_guid = GUID_zero();
304 struct netr_DsRGetDCNameInfo *info = NULL;
306 if (argc < 2) {
307 fprintf(stderr, "Usage: %s [client_account] [acb_mask] "
308 "[domain_name] [domain_guid] [site_name] "
309 "[flags]\n", argv[0]);
310 return WERR_OK;
313 if (argc >= 2) {
314 client_account = argv[1];
317 if (argc >= 3) {
318 mask = atoi(argv[2]);
321 if (argc >= 4) {
322 domain_name = argv[3];
325 if (argc >= 5) {
326 if (!NT_STATUS_IS_OK(GUID_from_string(argv[4], &domain_guid))) {
327 return WERR_NOMEM;
331 if (argc >= 6) {
332 site_name = argv[5];
335 if (argc >= 7) {
336 sscanf(argv[6], "%x", &flags);
339 debug_dsdcinfo_flags(1,flags);
341 status = rpccli_netr_DsRGetDCNameEx2(cli, mem_ctx,
342 server_name,
343 client_account,
344 mask,
345 domain_name,
346 &domain_guid,
347 site_name,
348 flags,
349 &info,
350 &result);
351 if (!NT_STATUS_IS_OK(status)) {
352 return ntstatus_to_werror(status);
355 if (!W_ERROR_IS_OK(result)) {
356 return result;
359 d_printf("DsRGetDCNameEx2 gave %s\n",
360 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
362 return result;
366 static WERROR cmd_netlogon_dsr_getsitename(struct rpc_pipe_client *cli,
367 TALLOC_CTX *mem_ctx, int argc,
368 const char **argv)
370 WERROR werr;
371 NTSTATUS status;
372 const char *sitename = NULL;
374 if (argc != 2) {
375 fprintf(stderr, "Usage: %s computername\n", argv[0]);
376 return WERR_OK;
379 status = rpccli_netr_DsRGetSiteName(cli, mem_ctx,
380 argv[1],
381 &sitename,
382 &werr);
383 if (!NT_STATUS_IS_OK(status)) {
384 return ntstatus_to_werror(status);
387 if (!W_ERROR_IS_OK(werr)) {
388 printf("rpccli_netlogon_dsr_gesitename returned %s\n",
389 nt_errstr(werror_to_ntstatus(werr)));
390 return werr;
393 printf("Computer %s is on Site: %s\n", argv[1], sitename);
395 return WERR_OK;
398 static WERROR cmd_netlogon_logon_ctrl(struct rpc_pipe_client *cli,
399 TALLOC_CTX *mem_ctx, int argc,
400 const char **argv)
402 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
403 WERROR werr;
404 const char *logon_server = cli->desthost;
405 enum netr_LogonControlCode function_code = 1;
406 uint32_t level = 1;
407 union netr_CONTROL_QUERY_INFORMATION info;
409 if (argc > 4) {
410 fprintf(stderr, "Usage: %s <logon_server> <function_code> "
411 "<level>\n", argv[0]);
412 return WERR_OK;
415 if (argc >= 2) {
416 logon_server = argv[1];
419 if (argc >= 3) {
420 function_code = atoi(argv[2]);
423 if (argc >= 4) {
424 level = atoi(argv[3]);
427 status = rpccli_netr_LogonControl(cli, mem_ctx,
428 logon_server,
429 function_code,
430 level,
431 &info,
432 &werr);
433 if (!NT_STATUS_IS_OK(status)) {
434 return ntstatus_to_werror(status);
437 if (!W_ERROR_IS_OK(werr)) {
438 return werr;
441 /* Display results */
443 return werr;
446 /* Display sam synchronisation information */
448 static void display_sam_sync(struct netr_DELTA_ENUM_ARRAY *r)
450 uint32_t i, j;
452 for (i=0; i < r->num_deltas; i++) {
454 union netr_DELTA_UNION u = r->delta_enum[i].delta_union;
455 union netr_DELTA_ID_UNION id = r->delta_enum[i].delta_id_union;
457 switch (r->delta_enum[i].delta_type) {
458 case NETR_DELTA_DOMAIN:
459 printf("Domain: %s\n",
460 u.domain->domain_name.string);
461 break;
462 case NETR_DELTA_GROUP:
463 printf("Group: %s\n",
464 u.group->group_name.string);
465 break;
466 case NETR_DELTA_DELETE_GROUP:
467 printf("Delete Group: %d\n",
468 u.delete_account.unknown);
469 break;
470 case NETR_DELTA_RENAME_GROUP:
471 printf("Rename Group: %s -> %s\n",
472 u.rename_group->OldName.string,
473 u.rename_group->NewName.string);
474 break;
475 case NETR_DELTA_USER:
476 printf("Account: %s\n",
477 u.user->account_name.string);
478 break;
479 case NETR_DELTA_DELETE_USER:
480 printf("Delete User: %d\n",
481 id.rid);
482 break;
483 case NETR_DELTA_RENAME_USER:
484 printf("Rename user: %s -> %s\n",
485 u.rename_user->OldName.string,
486 u.rename_user->NewName.string);
487 break;
488 case NETR_DELTA_GROUP_MEMBER:
489 for (j=0; j < u.group_member->num_rids; j++) {
490 printf("rid 0x%x, attrib 0x%08x\n",
491 u.group_member->rids[j],
492 u.group_member->attribs[j]);
494 break;
495 case NETR_DELTA_ALIAS:
496 printf("Alias: %s\n",
497 u.alias->alias_name.string);
498 break;
499 case NETR_DELTA_DELETE_ALIAS:
500 printf("Delete Alias: %d\n",
501 r->delta_enum[i].delta_id_union.rid);
502 break;
503 case NETR_DELTA_RENAME_ALIAS:
504 printf("Rename alias: %s -> %s\n",
505 u.rename_alias->OldName.string,
506 u.rename_alias->NewName.string);
507 break;
508 case NETR_DELTA_ALIAS_MEMBER:
509 for (j=0; j < u.alias_member->sids.num_sids; j++) {
510 fstring sid_str;
511 sid_to_fstring(sid_str,
512 u.alias_member->sids.sids[j].sid);
513 printf("%s\n", sid_str);
515 break;
516 case NETR_DELTA_POLICY:
517 printf("Policy\n");
518 break;
519 case NETR_DELTA_TRUSTED_DOMAIN:
520 printf("Trusted Domain: %s\n",
521 u.trusted_domain->domain_name.string);
522 break;
523 case NETR_DELTA_DELETE_TRUST:
524 printf("Delete Trust: %d\n",
525 u.delete_trust.unknown);
526 break;
527 case NETR_DELTA_ACCOUNT:
528 printf("Account\n");
529 break;
530 case NETR_DELTA_DELETE_ACCOUNT:
531 printf("Delete Account: %d\n",
532 u.delete_account.unknown);
533 break;
534 case NETR_DELTA_SECRET:
535 printf("Secret\n");
536 break;
537 case NETR_DELTA_DELETE_SECRET:
538 printf("Delete Secret: %d\n",
539 u.delete_secret.unknown);
540 break;
541 case NETR_DELTA_DELETE_GROUP2:
542 printf("Delete Group2: %s\n",
543 u.delete_group->account_name);
544 break;
545 case NETR_DELTA_DELETE_USER2:
546 printf("Delete User2: %s\n",
547 u.delete_user->account_name);
548 break;
549 case NETR_DELTA_MODIFY_COUNT:
550 printf("sam sequence update: 0x%016llx\n",
551 (unsigned long long) *u.modified_count);
552 break;
553 default:
554 printf("unknown delta type 0x%02x\n",
555 r->delta_enum[i].delta_type);
556 break;
561 /* Perform sam synchronisation */
563 static NTSTATUS cmd_netlogon_sam_sync(struct rpc_pipe_client *cli,
564 TALLOC_CTX *mem_ctx, int argc,
565 const char **argv)
567 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
568 const char *logon_server = cli->desthost;
569 const char *computername = global_myname();
570 struct netr_Authenticator credential;
571 struct netr_Authenticator return_authenticator;
572 enum netr_SamDatabaseID database_id = SAM_DATABASE_DOMAIN;
573 uint16_t restart_state = 0;
574 uint32_t sync_context = 0;
576 if (argc > 2) {
577 fprintf(stderr, "Usage: %s [database_id]\n", argv[0]);
578 return NT_STATUS_OK;
581 if (argc == 2) {
582 database_id = atoi(argv[1]);
585 /* Synchronise sam database */
587 do {
588 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
590 netlogon_creds_client_authenticator(cli->dc, &credential);
592 result = rpccli_netr_DatabaseSync2(cli, mem_ctx,
593 logon_server,
594 computername,
595 &credential,
596 &return_authenticator,
597 database_id,
598 restart_state,
599 &sync_context,
600 &delta_enum_array,
601 0xffff);
603 /* Check returned credentials. */
604 if (!netlogon_creds_client_check(cli->dc,
605 &return_authenticator.cred)) {
606 DEBUG(0,("credentials chain check failed\n"));
607 return NT_STATUS_ACCESS_DENIED;
610 if (NT_STATUS_IS_ERR(result)) {
611 break;
614 /* Display results */
616 display_sam_sync(delta_enum_array);
618 TALLOC_FREE(delta_enum_array);
620 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
622 return result;
625 /* Perform sam delta synchronisation */
627 static NTSTATUS cmd_netlogon_sam_deltas(struct rpc_pipe_client *cli,
628 TALLOC_CTX *mem_ctx, int argc,
629 const char **argv)
631 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
632 uint32_t tmp;
633 const char *logon_server = cli->desthost;
634 const char *computername = global_myname();
635 struct netr_Authenticator credential;
636 struct netr_Authenticator return_authenticator;
637 enum netr_SamDatabaseID database_id = SAM_DATABASE_DOMAIN;
638 uint64_t sequence_num;
640 if (argc != 3) {
641 fprintf(stderr, "Usage: %s database_id seqnum\n", argv[0]);
642 return NT_STATUS_OK;
645 database_id = atoi(argv[1]);
646 tmp = atoi(argv[2]);
648 sequence_num = tmp & 0xffff;
650 do {
651 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
653 netlogon_creds_client_authenticator(cli->dc, &credential);
655 result = rpccli_netr_DatabaseDeltas(cli, mem_ctx,
656 logon_server,
657 computername,
658 &credential,
659 &return_authenticator,
660 database_id,
661 &sequence_num,
662 &delta_enum_array,
663 0xffff);
665 /* Check returned credentials. */
666 if (!netlogon_creds_client_check(cli->dc,
667 &return_authenticator.cred)) {
668 DEBUG(0,("credentials chain check failed\n"));
669 return NT_STATUS_ACCESS_DENIED;
672 if (NT_STATUS_IS_ERR(result)) {
673 break;
676 /* Display results */
678 display_sam_sync(delta_enum_array);
680 TALLOC_FREE(delta_enum_array);
682 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
684 return result;
687 /* Log on a domain user */
689 static NTSTATUS cmd_netlogon_sam_logon(struct rpc_pipe_client *cli,
690 TALLOC_CTX *mem_ctx, int argc,
691 const char **argv)
693 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
694 int logon_type = NetlogonNetworkInformation;
695 const char *username, *password;
696 int auth_level = 2;
697 uint32 logon_param = 0;
698 const char *workstation = NULL;
700 /* Check arguments */
702 if (argc < 3 || argc > 7) {
703 fprintf(stderr, "Usage: samlogon <username> <password> [workstation]"
704 "[logon_type (1 or 2)] [auth level (2 or 3)] [logon_parameter]\n");
705 return NT_STATUS_OK;
708 username = argv[1];
709 password = argv[2];
711 if (argc >= 4)
712 workstation = argv[3];
714 if (argc >= 5)
715 sscanf(argv[4], "%i", &logon_type);
717 if (argc >= 6)
718 sscanf(argv[5], "%i", &auth_level);
720 if (argc == 7)
721 sscanf(argv[6], "%x", &logon_param);
723 /* Perform the sam logon */
725 result = rpccli_netlogon_sam_logon(cli, mem_ctx, logon_param, lp_workgroup(), username, password, workstation, logon_type);
727 if (!NT_STATUS_IS_OK(result))
728 goto done;
730 done:
731 return result;
734 /* Change the trust account password */
736 static NTSTATUS cmd_netlogon_change_trust_pw(struct rpc_pipe_client *cli,
737 TALLOC_CTX *mem_ctx, int argc,
738 const char **argv)
740 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
742 /* Check arguments */
744 if (argc > 1) {
745 fprintf(stderr, "Usage: change_trust_pw");
746 return NT_STATUS_OK;
749 /* Perform the sam logon */
751 result = trust_pw_find_change_and_store_it(cli, mem_ctx,
752 lp_workgroup());
754 if (!NT_STATUS_IS_OK(result))
755 goto done;
757 done:
758 return result;
761 static WERROR cmd_netlogon_gettrustrid(struct rpc_pipe_client *cli,
762 TALLOC_CTX *mem_ctx, int argc,
763 const char **argv)
765 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
766 WERROR werr = WERR_GENERAL_FAILURE;
767 const char *server_name = cli->desthost;
768 const char *domain_name = lp_workgroup();
769 uint32_t rid = 0;
771 if (argc < 1 || argc > 3) {
772 fprintf(stderr, "Usage: %s <server_name> <domain_name>\n",
773 argv[0]);
774 return WERR_OK;
777 if (argc >= 2) {
778 server_name = argv[1];
781 if (argc >= 3) {
782 domain_name = argv[2];
785 status = rpccli_netr_LogonGetTrustRid(cli, mem_ctx,
786 server_name,
787 domain_name,
788 &rid,
789 &werr);
790 if (!NT_STATUS_IS_OK(status)) {
791 goto done;
794 if (W_ERROR_IS_OK(werr)) {
795 printf("Rid: %d\n", rid);
797 done:
798 return werr;
801 static WERROR cmd_netlogon_dsr_enumtrustdom(struct rpc_pipe_client *cli,
802 TALLOC_CTX *mem_ctx, int argc,
803 const char **argv)
805 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
806 WERROR werr = WERR_GENERAL_FAILURE;
807 const char *server_name = cli->desthost;
808 uint32_t trust_flags = NETR_TRUST_FLAG_IN_FOREST;
809 struct netr_DomainTrustList trusts;
811 if (argc < 1 || argc > 3) {
812 fprintf(stderr, "Usage: %s <server_name> <trust_flags>\n",
813 argv[0]);
814 return WERR_OK;
817 if (argc >= 2) {
818 server_name = argv[1];
821 if (argc >= 3) {
822 sscanf(argv[2], "%x", &trust_flags);
825 status = rpccli_netr_DsrEnumerateDomainTrusts(cli, mem_ctx,
826 server_name,
827 trust_flags,
828 &trusts,
829 &werr);
830 if (!NT_STATUS_IS_OK(status)) {
831 goto done;
834 if (W_ERROR_IS_OK(werr)) {
835 int i;
837 printf("%d domains returned\n", trusts.count);
839 for (i=0; i<trusts.count; i++ ) {
840 printf("%s (%s)\n",
841 trusts.array[i].dns_name,
842 trusts.array[i].netbios_name);
845 done:
846 return werr;
849 static WERROR cmd_netlogon_deregisterdnsrecords(struct rpc_pipe_client *cli,
850 TALLOC_CTX *mem_ctx, int argc,
851 const char **argv)
853 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
854 WERROR werr = WERR_GENERAL_FAILURE;
855 const char *server_name = cli->desthost;
856 const char *domain = lp_workgroup();
857 const char *dns_host = NULL;
859 if (argc < 1 || argc > 4) {
860 fprintf(stderr, "Usage: %s <server_name> <domain_name> "
861 "<dns_host>\n", argv[0]);
862 return WERR_OK;
865 if (argc >= 2) {
866 server_name = argv[1];
869 if (argc >= 3) {
870 domain = argv[2];
873 if (argc >= 4) {
874 dns_host = argv[3];
877 status = rpccli_netr_DsrDeregisterDNSHostRecords(cli, mem_ctx,
878 server_name,
879 domain,
880 NULL,
881 NULL,
882 dns_host,
883 &werr);
884 if (!NT_STATUS_IS_OK(status)) {
885 goto done;
888 if (W_ERROR_IS_OK(werr)) {
889 printf("success\n");
891 done:
892 return werr;
895 static WERROR cmd_netlogon_dsr_getforesttrustinfo(struct rpc_pipe_client *cli,
896 TALLOC_CTX *mem_ctx, int argc,
897 const char **argv)
899 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
900 WERROR werr = WERR_GENERAL_FAILURE;
901 const char *server_name = cli->desthost;
902 const char *trusted_domain_name = NULL;
903 struct lsa_ForestTrustInformation *info = NULL;
904 uint32_t flags = 0;
906 if (argc < 1 || argc > 4) {
907 fprintf(stderr, "Usage: %s <server_name> <trusted_domain_name> "
908 "<flags>\n", argv[0]);
909 return WERR_OK;
912 if (argc >= 2) {
913 server_name = argv[1];
916 if (argc >= 3) {
917 trusted_domain_name = argv[2];
920 if (argc >= 4) {
921 sscanf(argv[3], "%x", &flags);
924 status = rpccli_netr_DsRGetForestTrustInformation(cli, mem_ctx,
925 server_name,
926 trusted_domain_name,
927 flags,
928 &info,
929 &werr);
930 if (!NT_STATUS_IS_OK(status)) {
931 goto done;
934 if (W_ERROR_IS_OK(werr)) {
935 printf("success\n");
937 done:
938 return werr;
941 static NTSTATUS cmd_netlogon_enumtrusteddomains(struct rpc_pipe_client *cli,
942 TALLOC_CTX *mem_ctx, int argc,
943 const char **argv)
945 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
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 NT_STATUS_OK;
955 if (argc >= 2) {
956 server_name = argv[1];
959 status = rpccli_netr_NetrEnumerateTrustedDomains(cli, mem_ctx,
960 server_name,
961 &blob);
962 if (!NT_STATUS_IS_OK(status)) {
963 goto done;
966 printf("success\n");
967 dump_data(1, blob.data, blob.length);
968 done:
969 return status;
972 static WERROR cmd_netlogon_enumtrusteddomainsex(struct rpc_pipe_client *cli,
973 TALLOC_CTX *mem_ctx, int argc,
974 const char **argv)
976 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
977 WERROR werr = WERR_GENERAL_FAILURE;
978 const char *server_name = cli->desthost;
979 struct netr_DomainTrustList list;
981 if (argc < 1 || argc > 3) {
982 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
983 return WERR_OK;
986 if (argc >= 2) {
987 server_name = argv[1];
990 status = rpccli_netr_NetrEnumerateTrustedDomainsEx(cli, mem_ctx,
991 server_name,
992 &list,
993 &werr);
994 if (!NT_STATUS_IS_OK(status)) {
995 goto done;
998 if (W_ERROR_IS_OK(werr)) {
999 printf("success\n");
1001 done:
1002 return werr;
1005 static WERROR cmd_netlogon_getdcsitecoverage(struct rpc_pipe_client *cli,
1006 TALLOC_CTX *mem_ctx, int argc,
1007 const char **argv)
1009 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1010 WERROR werr = WERR_GENERAL_FAILURE;
1011 const char *server_name = cli->desthost;
1012 struct DcSitesCtr *ctr = NULL;
1014 if (argc < 1 || argc > 3) {
1015 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
1016 return WERR_OK;
1019 if (argc >= 2) {
1020 server_name = argv[1];
1023 status = rpccli_netr_DsrGetDcSiteCoverageW(cli, mem_ctx,
1024 server_name,
1025 &ctr,
1026 &werr);
1027 if (!NT_STATUS_IS_OK(status)) {
1028 goto done;
1031 if (W_ERROR_IS_OK(werr) && ctr->num_sites) {
1032 int i;
1033 printf("sites covered by this DC: %d\n", ctr->num_sites);
1034 for (i=0; i<ctr->num_sites; i++) {
1035 printf("%s\n", ctr->sites[i].string);
1038 done:
1039 return werr;
1042 static NTSTATUS cmd_netlogon_database_redo(struct rpc_pipe_client *cli,
1043 TALLOC_CTX *mem_ctx, int argc,
1044 const char **argv)
1046 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1047 const char *server_name = cli->desthost;
1048 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1049 struct netr_Authenticator clnt_creds, srv_cred;
1050 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1051 unsigned char trust_passwd_hash[16];
1052 enum netr_SchannelType sec_channel_type = 0;
1053 struct netr_ChangeLogEntry e;
1054 uint32_t rid = 500;
1056 if (argc > 2) {
1057 fprintf(stderr, "Usage: %s <user rid>\n", argv[0]);
1058 return NT_STATUS_OK;
1061 if (argc == 2) {
1062 sscanf(argv[1], "%d", &rid);
1065 if (!secrets_fetch_trust_account_password(lp_workgroup(),
1066 trust_passwd_hash,
1067 NULL, &sec_channel_type)) {
1068 return NT_STATUS_UNSUCCESSFUL;
1071 status = rpccli_netlogon_setup_creds(cli,
1072 server_name, /* server name */
1073 lp_workgroup(), /* domain */
1074 global_myname(), /* client name */
1075 global_myname(), /* machine account name */
1076 trust_passwd_hash,
1077 sec_channel_type,
1078 &neg_flags);
1080 if (!NT_STATUS_IS_OK(status)) {
1081 return status;
1084 netlogon_creds_client_authenticator(cli->dc, &clnt_creds);
1086 ZERO_STRUCT(e);
1088 e.object_rid = rid;
1089 e.db_index = SAM_DATABASE_DOMAIN;
1090 e.delta_type = NETR_DELTA_USER;
1092 status = rpccli_netr_DatabaseRedo(cli, mem_ctx,
1093 server_name,
1094 global_myname(),
1095 &clnt_creds,
1096 &srv_cred,
1098 0, /* is calculated automatically */
1099 &delta_enum_array);
1101 if (!netlogon_creds_client_check(cli->dc, &srv_cred.cred)) {
1102 DEBUG(0,("credentials chain check failed\n"));
1103 return NT_STATUS_ACCESS_DENIED;
1106 return status;
1109 static NTSTATUS cmd_netlogon_capabilities(struct rpc_pipe_client *cli,
1110 TALLOC_CTX *mem_ctx, int argc,
1111 const char **argv)
1113 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1114 struct netr_Authenticator credential;
1115 struct netr_Authenticator return_authenticator;
1116 union netr_Capabilities capabilities;
1117 uint32_t level = 1;
1119 if (argc > 2) {
1120 fprintf(stderr, "Usage: %s <level>\n", argv[0]);
1121 return NT_STATUS_OK;
1124 if (argc == 2) {
1125 level = atoi(argv[1]);
1128 ZERO_STRUCT(return_authenticator);
1130 netlogon_creds_client_authenticator(cli->dc, &credential);
1132 status = rpccli_netr_LogonGetCapabilities(cli, mem_ctx,
1133 cli->desthost,
1134 global_myname(),
1135 &credential,
1136 &return_authenticator,
1137 level,
1138 &capabilities);
1140 if (!netlogon_creds_client_check(cli->dc,
1141 &return_authenticator.cred)) {
1142 DEBUG(0,("credentials chain check failed\n"));
1143 return NT_STATUS_ACCESS_DENIED;
1146 printf("capabilities: 0x%08x\n", capabilities.server_capabilities);
1148 return status;
1151 /* List of commands exported by this module */
1153 struct cmd_set netlogon_commands[] = {
1155 { "NETLOGON" },
1157 { "logonctrl2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl2, &ndr_table_netlogon.syntax_id, NULL, "Logon Control 2", "" },
1158 { "getanydcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getanydcname, &ndr_table_netlogon.syntax_id, NULL, "Get trusted DC name", "" },
1159 { "getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcname, &ndr_table_netlogon.syntax_id, NULL, "Get trusted PDC name", "" },
1160 { "dsr_getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcname, &ndr_table_netlogon.syntax_id, NULL, "Get trusted DC name", "" },
1161 { "dsr_getdcnameex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex, &ndr_table_netlogon.syntax_id, NULL, "Get trusted DC name", "" },
1162 { "dsr_getdcnameex2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex2, &ndr_table_netlogon.syntax_id, NULL, "Get trusted DC name", "" },
1163 { "dsr_getsitename", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getsitename, &ndr_table_netlogon.syntax_id, NULL, "Get sitename", "" },
1164 { "dsr_getforesttrustinfo", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getforesttrustinfo, &ndr_table_netlogon.syntax_id, NULL, "Get Forest Trust Info", "" },
1165 { "logonctrl", RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl, &ndr_table_netlogon.syntax_id, NULL, "Logon Control", "" },
1166 { "samsync", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_sync, NULL, &ndr_table_netlogon.syntax_id, NULL, "Sam Synchronisation", "" },
1167 { "samdeltas", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_deltas, NULL, &ndr_table_netlogon.syntax_id, NULL, "Query Sam Deltas", "" },
1168 { "samlogon", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_logon, NULL, &ndr_table_netlogon.syntax_id, NULL, "Sam Logon", "" },
1169 { "change_trust_pw", RPC_RTYPE_NTSTATUS, cmd_netlogon_change_trust_pw, NULL, &ndr_table_netlogon.syntax_id, NULL, "Change Trust Account Password", "" },
1170 { "gettrustrid", RPC_RTYPE_WERROR, NULL, cmd_netlogon_gettrustrid, &ndr_table_netlogon.syntax_id, NULL, "Get trust rid", "" },
1171 { "dsr_enumtrustdom", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon.syntax_id, NULL, "Enumerate trusted domains", "" },
1172 { "dsenumdomtrusts", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon.syntax_id, NULL, "Enumerate all trusted domains in an AD forest", "" },
1173 { "deregisterdnsrecords", RPC_RTYPE_WERROR, NULL, cmd_netlogon_deregisterdnsrecords, &ndr_table_netlogon.syntax_id, NULL, "Deregister DNS records", "" },
1174 { "netrenumtrusteddomains", RPC_RTYPE_NTSTATUS, cmd_netlogon_enumtrusteddomains, NULL, &ndr_table_netlogon.syntax_id, NULL, "Enumerate trusted domains", "" },
1175 { "netrenumtrusteddomainsex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_enumtrusteddomainsex, &ndr_table_netlogon.syntax_id, NULL, "Enumerate trusted domains", "" },
1176 { "getdcsitecoverage", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcsitecoverage, &ndr_table_netlogon.syntax_id, NULL, "Get the Site-Coverage from a DC", "" },
1177 { "database_redo", RPC_RTYPE_NTSTATUS, cmd_netlogon_database_redo, NULL, &ndr_table_netlogon.syntax_id, NULL, "Replicate single object from a DC", "" },
1178 { "capabilities", RPC_RTYPE_NTSTATUS, cmd_netlogon_capabilities, NULL, &ndr_table_netlogon.syntax_id, NULL, "Return Capabilities", "" },
1180 { NULL }