s3:libsmb/cliconnect: make use of ntlmssp_is_anonymous()
[Samba/gebeck_regimport.git] / source3 / rpcclient / cmd_netlogon.c
blobbaf2f4f694c5b5c50b0bbb3c4804f20c0a35cee1
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/ndr_netlogon.h"
26 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
27 #include "rpc_client/cli_netlogon.h"
28 #include "secrets.h"
30 static WERROR cmd_netlogon_logon_ctrl2(struct rpc_pipe_client *cli,
31 TALLOC_CTX *mem_ctx, int argc,
32 const char **argv)
34 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
35 WERROR werr;
36 const char *logon_server = cli->desthost;
37 enum netr_LogonControlCode function_code = NETLOGON_CONTROL_REDISCOVER;
38 uint32_t level = 1;
39 union netr_CONTROL_DATA_INFORMATION data;
40 union netr_CONTROL_QUERY_INFORMATION query;
41 const char *domain = lp_workgroup();
42 struct dcerpc_binding_handle *b = cli->binding_handle;
44 if (argc > 5) {
45 fprintf(stderr, "Usage: %s <logon_server> <function_code> "
46 "<level> <domain>\n", argv[0]);
47 return WERR_OK;
50 if (argc >= 2) {
51 logon_server = argv[1];
54 if (argc >= 3) {
55 function_code = atoi(argv[2]);
58 if (argc >= 4) {
59 level = atoi(argv[3]);
62 if (argc >= 5) {
63 domain = argv[4];
66 switch (function_code) {
67 case NETLOGON_CONTROL_REDISCOVER:
68 case NETLOGON_CONTROL_TC_QUERY:
69 data.domain = domain;
70 break;
71 default:
72 break;
75 status = dcerpc_netr_LogonControl2(b, mem_ctx,
76 logon_server,
77 function_code,
78 level,
79 &data,
80 &query,
81 &werr);
82 if (!NT_STATUS_IS_OK(status)) {
83 return ntstatus_to_werror(status);
86 if (!W_ERROR_IS_OK(werr)) {
87 return werr;
90 /* Display results */
92 return werr;
95 static WERROR cmd_netlogon_getanydcname(struct rpc_pipe_client *cli,
96 TALLOC_CTX *mem_ctx, int argc,
97 const char **argv)
99 const char *dcname = NULL;
100 WERROR werr;
101 NTSTATUS status;
102 int old_timeout;
103 struct dcerpc_binding_handle *b = cli->binding_handle;
105 if (argc != 2) {
106 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
107 return WERR_OK;
110 /* Make sure to wait for our DC's reply */
111 old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
112 rpccli_set_timeout(cli, MAX(old_timeout, 30000)); /* At least 30 sec */
114 status = dcerpc_netr_GetAnyDCName(b, mem_ctx,
115 cli->desthost,
116 argv[1],
117 &dcname,
118 &werr);
119 rpccli_set_timeout(cli, old_timeout);
121 if (!NT_STATUS_IS_OK(status)) {
122 return ntstatus_to_werror(status);
125 if (!W_ERROR_IS_OK(werr)) {
126 return werr;
129 /* Display results */
131 printf("%s\n", dcname);
133 return werr;
136 static WERROR cmd_netlogon_getdcname(struct rpc_pipe_client *cli,
137 TALLOC_CTX *mem_ctx, int argc,
138 const char **argv)
140 const char *dcname = NULL;
141 NTSTATUS status;
142 WERROR werr;
143 int old_timeout;
144 struct dcerpc_binding_handle *b = cli->binding_handle;
146 if (argc != 2) {
147 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
148 return WERR_OK;
151 /* Make sure to wait for our DC's reply */
152 old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
153 rpccli_set_timeout(cli, MAX(30000, old_timeout)); /* At least 30 sec */
155 status = dcerpc_netr_GetDcName(b, mem_ctx,
156 cli->desthost,
157 argv[1],
158 &dcname,
159 &werr);
160 rpccli_set_timeout(cli, old_timeout);
162 if (!NT_STATUS_IS_OK(status)) {
163 return ntstatus_to_werror(status);
166 if (!W_ERROR_IS_OK(werr)) {
167 return werr;
170 /* Display results */
172 printf("%s\n", dcname);
174 return werr;
177 static WERROR cmd_netlogon_dsr_getdcname(struct rpc_pipe_client *cli,
178 TALLOC_CTX *mem_ctx, int argc,
179 const char **argv)
181 NTSTATUS result;
182 WERROR werr = WERR_OK;
183 uint32 flags = DS_RETURN_DNS_NAME;
184 const char *server_name = cli->desthost;
185 const char *domain_name;
186 struct GUID domain_guid = GUID_zero();
187 struct GUID site_guid = GUID_zero();
188 struct netr_DsRGetDCNameInfo *info = NULL;
189 struct dcerpc_binding_handle *b = cli->binding_handle;
191 if (argc < 2) {
192 fprintf(stderr, "Usage: %s [domain_name] [domain_guid] "
193 "[site_guid] [flags]\n", argv[0]);
194 return WERR_OK;
197 if (argc >= 2)
198 domain_name = argv[1];
200 if (argc >= 3) {
201 if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
202 return WERR_NOMEM;
206 if (argc >= 4) {
207 if (!NT_STATUS_IS_OK(GUID_from_string(argv[3], &site_guid))) {
208 return WERR_NOMEM;
212 if (argc >= 5)
213 sscanf(argv[4], "%x", &flags);
215 result = dcerpc_netr_DsRGetDCName(b, mem_ctx,
216 server_name,
217 domain_name,
218 &domain_guid,
219 &site_guid,
220 flags,
221 &info,
222 &werr);
223 if (!NT_STATUS_IS_OK(result)) {
224 return ntstatus_to_werror(result);
227 if (W_ERROR_IS_OK(werr)) {
228 d_printf("DsGetDcName gave: %s\n",
229 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
230 return WERR_OK;
233 printf("rpccli_netlogon_dsr_getdcname returned %s\n",
234 win_errstr(werr));
236 return werr;
239 static WERROR cmd_netlogon_dsr_getdcnameex(struct rpc_pipe_client *cli,
240 TALLOC_CTX *mem_ctx, int argc,
241 const char **argv)
243 WERROR result;
244 NTSTATUS status;
245 uint32_t flags = DS_RETURN_DNS_NAME;
246 const char *server_name = cli->desthost;
247 const char *domain_name;
248 const char *site_name = NULL;
249 struct GUID domain_guid = GUID_zero();
250 struct netr_DsRGetDCNameInfo *info = NULL;
251 struct dcerpc_binding_handle *b = cli->binding_handle;
253 if (argc < 2) {
254 fprintf(stderr, "Usage: %s [domain_name] [domain_guid] "
255 "[site_name] [flags]\n", argv[0]);
256 return WERR_OK;
259 domain_name = argv[1];
261 if (argc >= 3) {
262 if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
263 return WERR_NOMEM;
267 if (argc >= 4) {
268 site_name = argv[3];
271 if (argc >= 5) {
272 sscanf(argv[4], "%x", &flags);
275 status = dcerpc_netr_DsRGetDCNameEx(b, mem_ctx,
276 server_name,
277 domain_name,
278 &domain_guid,
279 site_name,
280 flags,
281 &info,
282 &result);
283 if (!NT_STATUS_IS_OK(status)) {
284 return ntstatus_to_werror(status);
287 if (!W_ERROR_IS_OK(result)) {
288 return result;
291 d_printf("DsRGetDCNameEx gave %s\n",
292 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
294 return result;
297 static WERROR cmd_netlogon_dsr_getdcnameex2(struct rpc_pipe_client *cli,
298 TALLOC_CTX *mem_ctx, int argc,
299 const char **argv)
301 WERROR result;
302 NTSTATUS status;
303 uint32_t flags = DS_RETURN_DNS_NAME;
304 const char *server_name = cli->desthost;
305 const char *domain_name = NULL;
306 const char *client_account = NULL;
307 uint32_t mask = 0;
308 const char *site_name = NULL;
309 struct GUID domain_guid = GUID_zero();
310 struct netr_DsRGetDCNameInfo *info = NULL;
311 struct dcerpc_binding_handle *b = cli->binding_handle;
313 if (argc < 2) {
314 fprintf(stderr, "Usage: %s [client_account] [acb_mask] "
315 "[domain_name] [domain_guid] [site_name] "
316 "[flags]\n", argv[0]);
317 return WERR_OK;
320 if (argc >= 2) {
321 client_account = argv[1];
324 if (argc >= 3) {
325 mask = atoi(argv[2]);
328 if (argc >= 4) {
329 domain_name = argv[3];
332 if (argc >= 5) {
333 if (!NT_STATUS_IS_OK(GUID_from_string(argv[4], &domain_guid))) {
334 return WERR_NOMEM;
338 if (argc >= 6) {
339 site_name = argv[5];
342 if (argc >= 7) {
343 sscanf(argv[6], "%x", &flags);
346 status = dcerpc_netr_DsRGetDCNameEx2(b, mem_ctx,
347 server_name,
348 client_account,
349 mask,
350 domain_name,
351 &domain_guid,
352 site_name,
353 flags,
354 &info,
355 &result);
356 if (!NT_STATUS_IS_OK(status)) {
357 return ntstatus_to_werror(status);
360 if (!W_ERROR_IS_OK(result)) {
361 return result;
364 d_printf("DsRGetDCNameEx2 gave %s\n",
365 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
367 return result;
371 static WERROR cmd_netlogon_dsr_getsitename(struct rpc_pipe_client *cli,
372 TALLOC_CTX *mem_ctx, int argc,
373 const char **argv)
375 WERROR werr;
376 NTSTATUS status;
377 const char *sitename = NULL;
378 struct dcerpc_binding_handle *b = cli->binding_handle;
380 if (argc != 2) {
381 fprintf(stderr, "Usage: %s computername\n", argv[0]);
382 return WERR_OK;
385 status = dcerpc_netr_DsRGetSiteName(b, mem_ctx,
386 argv[1],
387 &sitename,
388 &werr);
389 if (!NT_STATUS_IS_OK(status)) {
390 return ntstatus_to_werror(status);
393 if (!W_ERROR_IS_OK(werr)) {
394 printf("rpccli_netlogon_dsr_gesitename returned %s\n",
395 nt_errstr(werror_to_ntstatus(werr)));
396 return werr;
399 printf("Computer %s is on Site: %s\n", argv[1], sitename);
401 return WERR_OK;
404 static WERROR cmd_netlogon_logon_ctrl(struct rpc_pipe_client *cli,
405 TALLOC_CTX *mem_ctx, int argc,
406 const char **argv)
408 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
409 WERROR werr;
410 const char *logon_server = cli->desthost;
411 enum netr_LogonControlCode function_code = 1;
412 uint32_t level = 1;
413 union netr_CONTROL_QUERY_INFORMATION info;
414 struct dcerpc_binding_handle *b = cli->binding_handle;
416 if (argc > 4) {
417 fprintf(stderr, "Usage: %s <logon_server> <function_code> "
418 "<level>\n", argv[0]);
419 return WERR_OK;
422 if (argc >= 2) {
423 logon_server = argv[1];
426 if (argc >= 3) {
427 function_code = atoi(argv[2]);
430 if (argc >= 4) {
431 level = atoi(argv[3]);
434 status = dcerpc_netr_LogonControl(b, mem_ctx,
435 logon_server,
436 function_code,
437 level,
438 &info,
439 &werr);
440 if (!NT_STATUS_IS_OK(status)) {
441 return ntstatus_to_werror(status);
444 if (!W_ERROR_IS_OK(werr)) {
445 return werr;
448 /* Display results */
450 return werr;
453 /* Display sam synchronisation information */
455 static void display_sam_sync(struct netr_DELTA_ENUM_ARRAY *r)
457 uint32_t i, j;
459 for (i=0; i < r->num_deltas; i++) {
461 union netr_DELTA_UNION u = r->delta_enum[i].delta_union;
462 union netr_DELTA_ID_UNION id = r->delta_enum[i].delta_id_union;
464 switch (r->delta_enum[i].delta_type) {
465 case NETR_DELTA_DOMAIN:
466 printf("Domain: %s\n",
467 u.domain->domain_name.string);
468 break;
469 case NETR_DELTA_GROUP:
470 printf("Group: %s\n",
471 u.group->group_name.string);
472 break;
473 case NETR_DELTA_DELETE_GROUP:
474 printf("Delete Group: %d\n",
475 id.rid);
476 break;
477 case NETR_DELTA_RENAME_GROUP:
478 printf("Rename Group: %s -> %s\n",
479 u.rename_group->OldName.string,
480 u.rename_group->NewName.string);
481 break;
482 case NETR_DELTA_USER:
483 printf("Account: %s\n",
484 u.user->account_name.string);
485 break;
486 case NETR_DELTA_DELETE_USER:
487 printf("Delete User: %d\n",
488 id.rid);
489 break;
490 case NETR_DELTA_RENAME_USER:
491 printf("Rename user: %s -> %s\n",
492 u.rename_user->OldName.string,
493 u.rename_user->NewName.string);
494 break;
495 case NETR_DELTA_GROUP_MEMBER:
496 for (j=0; j < u.group_member->num_rids; j++) {
497 printf("rid 0x%x, attrib 0x%08x\n",
498 u.group_member->rids[j],
499 u.group_member->attribs[j]);
501 break;
502 case NETR_DELTA_ALIAS:
503 printf("Alias: %s\n",
504 u.alias->alias_name.string);
505 break;
506 case NETR_DELTA_DELETE_ALIAS:
507 printf("Delete Alias: %d\n",
508 id.rid);
509 break;
510 case NETR_DELTA_RENAME_ALIAS:
511 printf("Rename alias: %s -> %s\n",
512 u.rename_alias->OldName.string,
513 u.rename_alias->NewName.string);
514 break;
515 case NETR_DELTA_ALIAS_MEMBER:
516 for (j=0; j < u.alias_member->sids.num_sids; j++) {
517 fstring sid_str;
518 sid_to_fstring(sid_str,
519 u.alias_member->sids.sids[j].sid);
520 printf("%s\n", sid_str);
522 break;
523 case NETR_DELTA_POLICY:
524 printf("Policy: %s\n",
525 sid_string_dbg(id.sid));
526 break;
527 case NETR_DELTA_TRUSTED_DOMAIN:
528 printf("Trusted Domain: %s\n",
529 u.trusted_domain->domain_name.string);
530 break;
531 case NETR_DELTA_DELETE_TRUST:
532 printf("Delete Trust: %s\n",
533 sid_string_dbg(id.sid));
534 break;
535 case NETR_DELTA_ACCOUNT:
536 printf("Account: %s\n",
537 sid_string_dbg(id.sid));
538 break;
539 case NETR_DELTA_DELETE_ACCOUNT:
540 printf("Delete Account: %s\n",
541 sid_string_dbg(id.sid));
542 break;
543 case NETR_DELTA_SECRET:
544 printf("Secret: %s\n",
545 id.name);
546 break;
547 case NETR_DELTA_DELETE_SECRET:
548 printf("Delete Secret: %s\n",
549 id.name);
550 break;
551 case NETR_DELTA_DELETE_GROUP2:
552 printf("Delete Group2: %s\n",
553 u.delete_group->account_name);
554 break;
555 case NETR_DELTA_DELETE_USER2:
556 printf("Delete User2: %s\n",
557 u.delete_user->account_name);
558 break;
559 case NETR_DELTA_MODIFY_COUNT:
560 printf("sam sequence update: 0x%016llx\n",
561 (unsigned long long) *u.modified_count);
562 break;
563 default:
564 printf("unknown delta type 0x%02x\n",
565 r->delta_enum[i].delta_type);
566 break;
571 /* Perform sam synchronisation */
573 static NTSTATUS cmd_netlogon_sam_sync(struct rpc_pipe_client *cli,
574 TALLOC_CTX *mem_ctx, int argc,
575 const char **argv)
577 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
578 NTSTATUS status;
579 const char *logon_server = cli->desthost;
580 const char *computername = lp_netbios_name();
581 struct netr_Authenticator credential;
582 struct netr_Authenticator return_authenticator;
583 enum netr_SamDatabaseID database_id = SAM_DATABASE_DOMAIN;
584 uint16_t restart_state = 0;
585 uint32_t sync_context = 0;
586 struct dcerpc_binding_handle *b = cli->binding_handle;
588 if (argc > 2) {
589 fprintf(stderr, "Usage: %s [database_id]\n", argv[0]);
590 return NT_STATUS_OK;
593 if (argc == 2) {
594 database_id = atoi(argv[1]);
597 /* Synchronise sam database */
599 do {
600 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
602 netlogon_creds_client_authenticator(cli->dc, &credential);
604 status = dcerpc_netr_DatabaseSync2(b, mem_ctx,
605 logon_server,
606 computername,
607 &credential,
608 &return_authenticator,
609 database_id,
610 restart_state,
611 &sync_context,
612 &delta_enum_array,
613 0xffff,
614 &result);
615 if (!NT_STATUS_IS_OK(status)) {
616 return status;
619 /* Check returned credentials. */
620 if (!netlogon_creds_client_check(cli->dc,
621 &return_authenticator.cred)) {
622 DEBUG(0,("credentials chain check failed\n"));
623 return NT_STATUS_ACCESS_DENIED;
626 if (NT_STATUS_IS_ERR(result)) {
627 break;
630 /* Display results */
632 display_sam_sync(delta_enum_array);
634 TALLOC_FREE(delta_enum_array);
636 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
638 return result;
641 /* Perform sam delta synchronisation */
643 static NTSTATUS cmd_netlogon_sam_deltas(struct rpc_pipe_client *cli,
644 TALLOC_CTX *mem_ctx, int argc,
645 const char **argv)
647 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
648 NTSTATUS status;
649 uint32_t tmp;
650 const char *logon_server = cli->desthost;
651 const char *computername = lp_netbios_name();
652 struct netr_Authenticator credential;
653 struct netr_Authenticator return_authenticator;
654 enum netr_SamDatabaseID database_id = SAM_DATABASE_DOMAIN;
655 uint64_t sequence_num;
656 struct dcerpc_binding_handle *b = cli->binding_handle;
658 if (argc != 3) {
659 fprintf(stderr, "Usage: %s database_id seqnum\n", argv[0]);
660 return NT_STATUS_OK;
663 database_id = atoi(argv[1]);
664 tmp = atoi(argv[2]);
666 sequence_num = tmp & 0xffff;
668 do {
669 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
671 netlogon_creds_client_authenticator(cli->dc, &credential);
673 status = dcerpc_netr_DatabaseDeltas(b, mem_ctx,
674 logon_server,
675 computername,
676 &credential,
677 &return_authenticator,
678 database_id,
679 &sequence_num,
680 &delta_enum_array,
681 0xffff,
682 &result);
683 if (!NT_STATUS_IS_OK(status)) {
684 return status;
687 /* Check returned credentials. */
688 if (!netlogon_creds_client_check(cli->dc,
689 &return_authenticator.cred)) {
690 DEBUG(0,("credentials chain check failed\n"));
691 return NT_STATUS_ACCESS_DENIED;
694 if (NT_STATUS_IS_ERR(result)) {
695 break;
698 /* Display results */
700 display_sam_sync(delta_enum_array);
702 TALLOC_FREE(delta_enum_array);
704 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
706 return result;
709 /* Log on a domain user */
711 static NTSTATUS cmd_netlogon_sam_logon(struct rpc_pipe_client *cli,
712 TALLOC_CTX *mem_ctx, int argc,
713 const char **argv)
715 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
716 int logon_type = NetlogonNetworkInformation;
717 const char *username, *password;
718 uint16_t validation_level = 3;
719 uint32 logon_param = 0;
720 const char *workstation = NULL;
722 /* Check arguments */
724 if (argc < 3 || argc > 7) {
725 fprintf(stderr, "Usage: samlogon <username> <password> [workstation]"
726 "[logon_type (1 or 2)] [auth level (2 or 3)] [logon_parameter]\n");
727 return NT_STATUS_OK;
730 username = argv[1];
731 password = argv[2];
733 if (argc >= 4)
734 workstation = argv[3];
736 if (argc >= 5)
737 sscanf(argv[4], "%i", &logon_type);
739 if (argc >= 6)
740 validation_level = atoi(argv[5]);
742 if (argc == 7)
743 sscanf(argv[6], "%x", &logon_param);
745 /* Perform the sam logon */
747 result = rpccli_netlogon_sam_logon(cli, mem_ctx, logon_param, lp_workgroup(), username, password, workstation, validation_level, logon_type);
749 if (!NT_STATUS_IS_OK(result))
750 goto done;
752 done:
753 return result;
756 /* Change the trust account password */
758 static NTSTATUS cmd_netlogon_change_trust_pw(struct rpc_pipe_client *cli,
759 TALLOC_CTX *mem_ctx, int argc,
760 const char **argv)
762 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
764 /* Check arguments */
766 if (argc > 1) {
767 fprintf(stderr, "Usage: change_trust_pw");
768 return NT_STATUS_OK;
771 /* Perform the sam logon */
773 result = trust_pw_find_change_and_store_it(cli, mem_ctx,
774 lp_workgroup());
776 if (!NT_STATUS_IS_OK(result))
777 goto done;
779 done:
780 return result;
783 static WERROR cmd_netlogon_gettrustrid(struct rpc_pipe_client *cli,
784 TALLOC_CTX *mem_ctx, int argc,
785 const char **argv)
787 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
788 WERROR werr = WERR_GENERAL_FAILURE;
789 const char *server_name = cli->desthost;
790 const char *domain_name = lp_workgroup();
791 uint32_t rid = 0;
792 struct dcerpc_binding_handle *b = cli->binding_handle;
794 if (argc < 1 || argc > 3) {
795 fprintf(stderr, "Usage: %s <server_name> <domain_name>\n",
796 argv[0]);
797 return WERR_OK;
800 if (argc >= 2) {
801 server_name = argv[1];
804 if (argc >= 3) {
805 domain_name = argv[2];
808 status = dcerpc_netr_LogonGetTrustRid(b, mem_ctx,
809 server_name,
810 domain_name,
811 &rid,
812 &werr);
813 if (!NT_STATUS_IS_OK(status)) {
814 werr = ntstatus_to_werror(status);
815 goto done;
818 if (W_ERROR_IS_OK(werr)) {
819 printf("Rid: %d\n", rid);
821 done:
822 return werr;
825 static WERROR cmd_netlogon_dsr_enumtrustdom(struct rpc_pipe_client *cli,
826 TALLOC_CTX *mem_ctx, int argc,
827 const char **argv)
829 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
830 WERROR werr = WERR_GENERAL_FAILURE;
831 const char *server_name = cli->desthost;
832 uint32_t trust_flags = NETR_TRUST_FLAG_IN_FOREST;
833 struct netr_DomainTrustList trusts;
834 struct dcerpc_binding_handle *b = cli->binding_handle;
836 if (argc < 1 || argc > 3) {
837 fprintf(stderr, "Usage: %s <server_name> <trust_flags>\n",
838 argv[0]);
839 return WERR_OK;
842 if (argc >= 2) {
843 server_name = argv[1];
846 if (argc >= 3) {
847 sscanf(argv[2], "%x", &trust_flags);
850 status = dcerpc_netr_DsrEnumerateDomainTrusts(b, mem_ctx,
851 server_name,
852 trust_flags,
853 &trusts,
854 &werr);
855 if (!NT_STATUS_IS_OK(status)) {
856 werr = ntstatus_to_werror(status);
857 goto done;
860 if (W_ERROR_IS_OK(werr)) {
861 int i;
863 printf("%d domains returned\n", trusts.count);
865 for (i=0; i<trusts.count; i++ ) {
866 printf("%s (%s)\n",
867 trusts.array[i].dns_name,
868 trusts.array[i].netbios_name);
871 done:
872 return werr;
875 static WERROR cmd_netlogon_deregisterdnsrecords(struct rpc_pipe_client *cli,
876 TALLOC_CTX *mem_ctx, int argc,
877 const char **argv)
879 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
880 WERROR werr = WERR_GENERAL_FAILURE;
881 const char *server_name = cli->desthost;
882 const char *domain = lp_workgroup();
883 const char *dns_host = NULL;
884 struct dcerpc_binding_handle *b = cli->binding_handle;
886 if (argc < 1 || argc > 4) {
887 fprintf(stderr, "Usage: %s <server_name> <domain_name> "
888 "<dns_host>\n", argv[0]);
889 return WERR_OK;
892 if (argc >= 2) {
893 server_name = argv[1];
896 if (argc >= 3) {
897 domain = argv[2];
900 if (argc >= 4) {
901 dns_host = argv[3];
904 status = dcerpc_netr_DsrDeregisterDNSHostRecords(b, mem_ctx,
905 server_name,
906 domain,
907 NULL,
908 NULL,
909 dns_host,
910 &werr);
911 if (!NT_STATUS_IS_OK(status)) {
912 werr = ntstatus_to_werror(status);
913 goto done;
916 if (W_ERROR_IS_OK(werr)) {
917 printf("success\n");
919 done:
920 return werr;
923 static WERROR cmd_netlogon_dsr_getforesttrustinfo(struct rpc_pipe_client *cli,
924 TALLOC_CTX *mem_ctx, int argc,
925 const char **argv)
927 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
928 WERROR werr = WERR_GENERAL_FAILURE;
929 const char *server_name = cli->desthost;
930 const char *trusted_domain_name = NULL;
931 struct lsa_ForestTrustInformation *info = NULL;
932 uint32_t flags = 0;
933 struct dcerpc_binding_handle *b = cli->binding_handle;
935 if (argc < 1 || argc > 4) {
936 fprintf(stderr, "Usage: %s <server_name> <trusted_domain_name> "
937 "<flags>\n", argv[0]);
938 return WERR_OK;
941 if (argc >= 2) {
942 server_name = argv[1];
945 if (argc >= 3) {
946 trusted_domain_name = argv[2];
949 if (argc >= 4) {
950 sscanf(argv[3], "%x", &flags);
953 status = dcerpc_netr_DsRGetForestTrustInformation(b, mem_ctx,
954 server_name,
955 trusted_domain_name,
956 flags,
957 &info,
958 &werr);
959 if (!NT_STATUS_IS_OK(status)) {
960 werr = ntstatus_to_werror(status);
961 goto done;
964 if (W_ERROR_IS_OK(werr)) {
965 printf("success\n");
967 done:
968 return werr;
971 static NTSTATUS cmd_netlogon_enumtrusteddomains(struct rpc_pipe_client *cli,
972 TALLOC_CTX *mem_ctx, int argc,
973 const char **argv)
975 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
976 NTSTATUS result;
977 const char *server_name = cli->desthost;
978 struct netr_Blob blob;
979 struct dcerpc_binding_handle *b = cli->binding_handle;
982 if (argc < 1 || argc > 3) {
983 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
984 return NT_STATUS_OK;
987 if (argc >= 2) {
988 server_name = argv[1];
991 status = dcerpc_netr_NetrEnumerateTrustedDomains(b, mem_ctx,
992 server_name,
993 &blob,
994 &result);
995 if (!NT_STATUS_IS_OK(status)) {
996 goto done;
999 if (!NT_STATUS_IS_OK(result)) {
1000 status = result;
1001 goto done;
1004 printf("success\n");
1005 dump_data(1, blob.data, blob.length);
1006 done:
1007 return status;
1010 static WERROR cmd_netlogon_enumtrusteddomainsex(struct rpc_pipe_client *cli,
1011 TALLOC_CTX *mem_ctx, int argc,
1012 const char **argv)
1014 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1015 WERROR werr = WERR_GENERAL_FAILURE;
1016 const char *server_name = cli->desthost;
1017 struct netr_DomainTrustList list;
1018 struct dcerpc_binding_handle *b = cli->binding_handle;
1020 if (argc < 1 || argc > 3) {
1021 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
1022 return WERR_OK;
1025 if (argc >= 2) {
1026 server_name = argv[1];
1029 status = dcerpc_netr_NetrEnumerateTrustedDomainsEx(b, mem_ctx,
1030 server_name,
1031 &list,
1032 &werr);
1033 if (!NT_STATUS_IS_OK(status)) {
1034 werr = ntstatus_to_werror(status);
1035 goto done;
1038 if (W_ERROR_IS_OK(werr)) {
1039 printf("success\n");
1041 done:
1042 return werr;
1045 static WERROR cmd_netlogon_getdcsitecoverage(struct rpc_pipe_client *cli,
1046 TALLOC_CTX *mem_ctx, int argc,
1047 const char **argv)
1049 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1050 WERROR werr = WERR_GENERAL_FAILURE;
1051 const char *server_name = cli->desthost;
1052 struct DcSitesCtr *ctr = NULL;
1053 struct dcerpc_binding_handle *b = cli->binding_handle;
1055 if (argc < 1 || argc > 3) {
1056 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
1057 return WERR_OK;
1060 if (argc >= 2) {
1061 server_name = argv[1];
1064 status = dcerpc_netr_DsrGetDcSiteCoverageW(b, mem_ctx,
1065 server_name,
1066 &ctr,
1067 &werr);
1068 if (!NT_STATUS_IS_OK(status)) {
1069 werr = ntstatus_to_werror(status);
1070 goto done;
1073 if (W_ERROR_IS_OK(werr) && ctr->num_sites) {
1074 int i;
1075 printf("sites covered by this DC: %d\n", ctr->num_sites);
1076 for (i=0; i<ctr->num_sites; i++) {
1077 printf("%s\n", ctr->sites[i].string);
1080 done:
1081 return werr;
1084 static NTSTATUS cmd_netlogon_database_redo(struct rpc_pipe_client *cli,
1085 TALLOC_CTX *mem_ctx, int argc,
1086 const char **argv)
1088 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1089 NTSTATUS result;
1090 const char *server_name = cli->desthost;
1091 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1092 struct netr_Authenticator clnt_creds, srv_cred;
1093 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1094 unsigned char trust_passwd_hash[16];
1095 enum netr_SchannelType sec_channel_type = 0;
1096 struct netr_ChangeLogEntry e;
1097 uint32_t rid = 500;
1098 struct dcerpc_binding_handle *b = cli->binding_handle;
1100 if (argc > 2) {
1101 fprintf(stderr, "Usage: %s <user rid>\n", argv[0]);
1102 return NT_STATUS_OK;
1105 if (argc == 2) {
1106 sscanf(argv[1], "%d", &rid);
1109 if (!secrets_fetch_trust_account_password(lp_workgroup(),
1110 trust_passwd_hash,
1111 NULL, &sec_channel_type)) {
1112 return NT_STATUS_UNSUCCESSFUL;
1115 status = rpccli_netlogon_setup_creds(cli,
1116 server_name, /* server name */
1117 lp_workgroup(), /* domain */
1118 lp_netbios_name(), /* client name */
1119 lp_netbios_name(), /* machine account name */
1120 trust_passwd_hash,
1121 sec_channel_type,
1122 &neg_flags);
1124 if (!NT_STATUS_IS_OK(status)) {
1125 return status;
1128 netlogon_creds_client_authenticator(cli->dc, &clnt_creds);
1130 ZERO_STRUCT(e);
1132 e.object_rid = rid;
1133 e.db_index = SAM_DATABASE_DOMAIN;
1134 e.delta_type = NETR_DELTA_USER;
1136 status = dcerpc_netr_DatabaseRedo(b, mem_ctx,
1137 server_name,
1138 lp_netbios_name(),
1139 &clnt_creds,
1140 &srv_cred,
1142 0, /* is calculated automatically */
1143 &delta_enum_array,
1144 &result);
1145 if (!NT_STATUS_IS_OK(status)) {
1146 return status;
1149 if (!netlogon_creds_client_check(cli->dc, &srv_cred.cred)) {
1150 DEBUG(0,("credentials chain check failed\n"));
1151 return NT_STATUS_ACCESS_DENIED;
1154 return result;
1157 static NTSTATUS cmd_netlogon_capabilities(struct rpc_pipe_client *cli,
1158 TALLOC_CTX *mem_ctx, int argc,
1159 const char **argv)
1161 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1162 NTSTATUS result;
1163 struct netr_Authenticator credential;
1164 struct netr_Authenticator return_authenticator;
1165 union netr_Capabilities capabilities;
1166 uint32_t level = 1;
1167 struct dcerpc_binding_handle *b = cli->binding_handle;
1169 if (argc > 2) {
1170 fprintf(stderr, "Usage: %s <level>\n", argv[0]);
1171 return NT_STATUS_OK;
1174 if (argc == 2) {
1175 level = atoi(argv[1]);
1178 ZERO_STRUCT(return_authenticator);
1180 netlogon_creds_client_authenticator(cli->dc, &credential);
1182 status = dcerpc_netr_LogonGetCapabilities(b, mem_ctx,
1183 cli->desthost,
1184 lp_netbios_name(),
1185 &credential,
1186 &return_authenticator,
1187 level,
1188 &capabilities,
1189 &result);
1190 if (!NT_STATUS_IS_OK(status)) {
1191 return status;
1194 if (!netlogon_creds_client_check(cli->dc,
1195 &return_authenticator.cred)) {
1196 DEBUG(0,("credentials chain check failed\n"));
1197 return NT_STATUS_ACCESS_DENIED;
1200 printf("capabilities: 0x%08x\n", capabilities.server_capabilities);
1202 return result;
1205 /* List of commands exported by this module */
1207 struct cmd_set netlogon_commands[] = {
1209 { "NETLOGON" },
1211 { "logonctrl2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl2, &ndr_table_netlogon, NULL, "Logon Control 2", "" },
1212 { "getanydcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getanydcname, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1213 { "getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcname, &ndr_table_netlogon, NULL, "Get trusted PDC name", "" },
1214 { "dsr_getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcname, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1215 { "dsr_getdcnameex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1216 { "dsr_getdcnameex2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex2, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1217 { "dsr_getsitename", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getsitename, &ndr_table_netlogon, NULL, "Get sitename", "" },
1218 { "dsr_getforesttrustinfo", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getforesttrustinfo, &ndr_table_netlogon, NULL, "Get Forest Trust Info", "" },
1219 { "logonctrl", RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl, &ndr_table_netlogon, NULL, "Logon Control", "" },
1220 { "samsync", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_sync, NULL, &ndr_table_netlogon, NULL, "Sam Synchronisation", "" },
1221 { "samdeltas", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_deltas, NULL, &ndr_table_netlogon, NULL, "Query Sam Deltas", "" },
1222 { "samlogon", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_logon, NULL, &ndr_table_netlogon, NULL, "Sam Logon", "" },
1223 { "change_trust_pw", RPC_RTYPE_NTSTATUS, cmd_netlogon_change_trust_pw, NULL, &ndr_table_netlogon, NULL, "Change Trust Account Password", "" },
1224 { "gettrustrid", RPC_RTYPE_WERROR, NULL, cmd_netlogon_gettrustrid, &ndr_table_netlogon, NULL, "Get trust rid", "" },
1225 { "dsr_enumtrustdom", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon, NULL, "Enumerate trusted domains", "" },
1226 { "dsenumdomtrusts", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon, NULL, "Enumerate all trusted domains in an AD forest", "" },
1227 { "deregisterdnsrecords", RPC_RTYPE_WERROR, NULL, cmd_netlogon_deregisterdnsrecords, &ndr_table_netlogon, NULL, "Deregister DNS records", "" },
1228 { "netrenumtrusteddomains", RPC_RTYPE_NTSTATUS, cmd_netlogon_enumtrusteddomains, NULL, &ndr_table_netlogon, NULL, "Enumerate trusted domains", "" },
1229 { "netrenumtrusteddomainsex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_enumtrusteddomainsex, &ndr_table_netlogon, NULL, "Enumerate trusted domains", "" },
1230 { "getdcsitecoverage", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcsitecoverage, &ndr_table_netlogon, NULL, "Get the Site-Coverage from a DC", "" },
1231 { "database_redo", RPC_RTYPE_NTSTATUS, cmd_netlogon_database_redo, NULL, &ndr_table_netlogon, NULL, "Replicate single object from a DC", "" },
1232 { "capabilities", RPC_RTYPE_NTSTATUS, cmd_netlogon_capabilities, NULL, &ndr_table_netlogon, NULL, "Return Capabilities", "" },
1234 { NULL }