ctdb-recoverd: Gently abort recovery when election is underway
[Samba.git] / source3 / rpcclient / cmd_netlogon.c
blob2d1c35168cea0f28b068decbe933a5ac528267a6
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"
29 #include "../libcli/auth/netlogon_creds_cli.h"
31 static WERROR cmd_netlogon_logon_ctrl2(struct rpc_pipe_client *cli,
32 TALLOC_CTX *mem_ctx, int argc,
33 const char **argv)
35 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
36 WERROR werr;
37 const char *logon_server = cli->desthost;
38 enum netr_LogonControlCode function_code = NETLOGON_CONTROL_REDISCOVER;
39 uint32_t level = 1;
40 union netr_CONTROL_DATA_INFORMATION data;
41 union netr_CONTROL_QUERY_INFORMATION query;
42 const char *domain = lp_workgroup();
43 struct dcerpc_binding_handle *b = cli->binding_handle;
44 int i;
45 #define fn_code_level(x, item) { x, #x, #item }
46 struct {
47 enum netr_LogonControlCode code;
48 const char *name;
49 const char *argument;
50 } supported_levels[] = {
51 fn_code_level(NETLOGON_CONTROL_REDISCOVER, domain),
52 fn_code_level(NETLOGON_CONTROL_TC_QUERY, domain),
53 fn_code_level(NETLOGON_CONTROL_TRANSPORT_NOTIFY, domain),
54 fn_code_level(NETLOGON_CONTROL_FIND_USER, user),
55 fn_code_level(NETLOGON_CONTROL_CHANGE_PASSWORD, domain),
56 fn_code_level(NETLOGON_CONTROL_TC_VERIFY, domain),
57 fn_code_level(NETLOGON_CONTROL_SET_DBFLAG, debug_level),
58 {0, 0, 0}
60 #undef fn_code_level
61 if ((argc > 5) || (argc < 2)) {
62 fprintf(stderr, "Usage: %s <logon_server> <function_code> "
63 "<level:1..4> <argument>\n", argv[0]);
64 fprintf(stderr, "Supported combinations:\n");
65 fprintf(stderr, "function_code\targument\n");
66 for(i=0; supported_levels[i].code; i++) {
67 fprintf(stderr, "%7d\t\t%s\t(%s)\n",
68 supported_levels[i].code,
69 supported_levels[i].argument,
70 supported_levels[i].name);
72 return WERR_OK;
75 if (argc >= 2) {
76 logon_server = argv[1];
79 if (argc >= 3) {
80 function_code = atoi(argv[2]);
83 if (argc >= 4) {
84 level = atoi(argv[3]);
87 if (argc >= 5) {
88 domain = argv[4];
91 switch (function_code) {
92 case NETLOGON_CONTROL_REDISCOVER:
93 case NETLOGON_CONTROL_TC_QUERY:
94 case NETLOGON_CONTROL_CHANGE_PASSWORD:
95 case NETLOGON_CONTROL_TRANSPORT_NOTIFY:
96 case NETLOGON_CONTROL_TC_VERIFY:
97 data.domain = domain;
98 break;
99 case NETLOGON_CONTROL_FIND_USER:
100 data.user = domain;
101 break;
102 case NETLOGON_CONTROL_SET_DBFLAG:
103 data.debug_level = atoi(domain);
104 default:
105 break;
108 status = dcerpc_netr_LogonControl2(b, mem_ctx,
109 logon_server,
110 function_code,
111 level,
112 &data,
113 &query,
114 &werr);
115 if (!NT_STATUS_IS_OK(status)) {
116 return ntstatus_to_werror(status);
119 if (!W_ERROR_IS_OK(werr)) {
120 return werr;
123 /* Display results */
125 return werr;
128 static WERROR cmd_netlogon_getanydcname(struct rpc_pipe_client *cli,
129 TALLOC_CTX *mem_ctx, int argc,
130 const char **argv)
132 const char *dcname = NULL;
133 WERROR werr;
134 NTSTATUS status;
135 int old_timeout;
136 struct dcerpc_binding_handle *b = cli->binding_handle;
138 if (argc != 2) {
139 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
140 return WERR_OK;
143 /* Make sure to wait for our DC's reply */
144 old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
145 rpccli_set_timeout(cli, MAX(old_timeout, 30000)); /* At least 30 sec */
147 status = dcerpc_netr_GetAnyDCName(b, mem_ctx,
148 cli->desthost,
149 argv[1],
150 &dcname,
151 &werr);
152 rpccli_set_timeout(cli, old_timeout);
154 if (!NT_STATUS_IS_OK(status)) {
155 return ntstatus_to_werror(status);
158 if (!W_ERROR_IS_OK(werr)) {
159 return werr;
162 /* Display results */
164 printf("%s\n", dcname);
166 return werr;
169 static WERROR cmd_netlogon_getdcname(struct rpc_pipe_client *cli,
170 TALLOC_CTX *mem_ctx, int argc,
171 const char **argv)
173 const char *dcname = NULL;
174 NTSTATUS status;
175 WERROR werr;
176 int old_timeout;
177 struct dcerpc_binding_handle *b = cli->binding_handle;
179 if (argc != 2) {
180 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
181 return WERR_OK;
184 /* Make sure to wait for our DC's reply */
185 old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
186 rpccli_set_timeout(cli, MAX(30000, old_timeout)); /* At least 30 sec */
188 status = dcerpc_netr_GetDcName(b, mem_ctx,
189 cli->desthost,
190 argv[1],
191 &dcname,
192 &werr);
193 rpccli_set_timeout(cli, old_timeout);
195 if (!NT_STATUS_IS_OK(status)) {
196 return ntstatus_to_werror(status);
199 if (!W_ERROR_IS_OK(werr)) {
200 return werr;
203 /* Display results */
205 printf("%s\n", dcname);
207 return werr;
210 static WERROR cmd_netlogon_dsr_getdcname(struct rpc_pipe_client *cli,
211 TALLOC_CTX *mem_ctx, int argc,
212 const char **argv)
214 NTSTATUS result;
215 WERROR werr = WERR_OK;
216 uint32 flags = DS_RETURN_DNS_NAME;
217 const char *server_name = cli->desthost;
218 const char *domain_name;
219 struct GUID domain_guid = GUID_zero();
220 struct GUID site_guid = GUID_zero();
221 struct netr_DsRGetDCNameInfo *info = NULL;
222 struct dcerpc_binding_handle *b = cli->binding_handle;
224 if (argc < 2) {
225 fprintf(stderr, "Usage: %s [domain_name] [domain_guid] "
226 "[site_guid] [flags]\n", argv[0]);
227 return WERR_OK;
230 if (argc >= 2)
231 domain_name = argv[1];
233 if (argc >= 3) {
234 if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
235 return WERR_NOMEM;
239 if (argc >= 4) {
240 if (!NT_STATUS_IS_OK(GUID_from_string(argv[3], &site_guid))) {
241 return WERR_NOMEM;
245 if (argc >= 5)
246 sscanf(argv[4], "%x", &flags);
248 result = dcerpc_netr_DsRGetDCName(b, mem_ctx,
249 server_name,
250 domain_name,
251 &domain_guid,
252 &site_guid,
253 flags,
254 &info,
255 &werr);
256 if (!NT_STATUS_IS_OK(result)) {
257 return ntstatus_to_werror(result);
260 if (W_ERROR_IS_OK(werr)) {
261 d_printf("DsGetDcName gave: %s\n",
262 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
263 return WERR_OK;
266 printf("rpccli_netlogon_dsr_getdcname returned %s\n",
267 win_errstr(werr));
269 return werr;
272 static WERROR cmd_netlogon_dsr_getdcnameex(struct rpc_pipe_client *cli,
273 TALLOC_CTX *mem_ctx, int argc,
274 const char **argv)
276 WERROR result;
277 NTSTATUS status;
278 uint32_t flags = DS_RETURN_DNS_NAME;
279 const char *server_name = cli->desthost;
280 const char *domain_name;
281 const char *site_name = NULL;
282 struct GUID domain_guid = GUID_zero();
283 struct netr_DsRGetDCNameInfo *info = NULL;
284 struct dcerpc_binding_handle *b = cli->binding_handle;
286 if (argc < 2) {
287 fprintf(stderr, "Usage: %s [domain_name] [domain_guid] "
288 "[site_name] [flags]\n", argv[0]);
289 return WERR_OK;
292 domain_name = argv[1];
294 if (argc >= 3) {
295 if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
296 return WERR_NOMEM;
300 if (argc >= 4) {
301 site_name = argv[3];
304 if (argc >= 5) {
305 sscanf(argv[4], "%x", &flags);
308 status = dcerpc_netr_DsRGetDCNameEx(b, mem_ctx,
309 server_name,
310 domain_name,
311 &domain_guid,
312 site_name,
313 flags,
314 &info,
315 &result);
316 if (!NT_STATUS_IS_OK(status)) {
317 return ntstatus_to_werror(status);
320 if (!W_ERROR_IS_OK(result)) {
321 return result;
324 d_printf("DsRGetDCNameEx gave %s\n",
325 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
327 return result;
330 static WERROR cmd_netlogon_dsr_getdcnameex2(struct rpc_pipe_client *cli,
331 TALLOC_CTX *mem_ctx, int argc,
332 const char **argv)
334 WERROR result;
335 NTSTATUS status;
336 uint32_t flags = DS_RETURN_DNS_NAME;
337 const char *server_name = cli->desthost;
338 const char *domain_name = NULL;
339 const char *client_account = NULL;
340 uint32_t mask = 0;
341 const char *site_name = NULL;
342 struct GUID domain_guid = GUID_zero();
343 struct netr_DsRGetDCNameInfo *info = NULL;
344 struct dcerpc_binding_handle *b = cli->binding_handle;
346 if (argc < 2) {
347 fprintf(stderr, "Usage: %s [client_account] [acb_mask] "
348 "[domain_name] [domain_guid] [site_name] "
349 "[flags]\n", argv[0]);
350 return WERR_OK;
353 if (argc >= 2) {
354 client_account = argv[1];
357 if (argc >= 3) {
358 mask = atoi(argv[2]);
361 if (argc >= 4) {
362 domain_name = argv[3];
365 if (argc >= 5) {
366 if (!NT_STATUS_IS_OK(GUID_from_string(argv[4], &domain_guid))) {
367 return WERR_NOMEM;
371 if (argc >= 6) {
372 site_name = argv[5];
375 if (argc >= 7) {
376 sscanf(argv[6], "%x", &flags);
379 status = dcerpc_netr_DsRGetDCNameEx2(b, mem_ctx,
380 server_name,
381 client_account,
382 mask,
383 domain_name,
384 &domain_guid,
385 site_name,
386 flags,
387 &info,
388 &result);
389 if (!NT_STATUS_IS_OK(status)) {
390 return ntstatus_to_werror(status);
393 if (!W_ERROR_IS_OK(result)) {
394 return result;
397 d_printf("DsRGetDCNameEx2 gave %s\n",
398 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
400 return result;
404 static WERROR cmd_netlogon_dsr_getsitename(struct rpc_pipe_client *cli,
405 TALLOC_CTX *mem_ctx, int argc,
406 const char **argv)
408 WERROR werr;
409 NTSTATUS status;
410 const char *sitename = NULL;
411 struct dcerpc_binding_handle *b = cli->binding_handle;
413 if (argc != 2) {
414 fprintf(stderr, "Usage: %s computername\n", argv[0]);
415 return WERR_OK;
418 status = dcerpc_netr_DsRGetSiteName(b, mem_ctx,
419 argv[1],
420 &sitename,
421 &werr);
422 if (!NT_STATUS_IS_OK(status)) {
423 return ntstatus_to_werror(status);
426 if (!W_ERROR_IS_OK(werr)) {
427 printf("rpccli_netlogon_dsr_gesitename returned %s\n",
428 nt_errstr(werror_to_ntstatus(werr)));
429 return werr;
432 printf("Computer %s is on Site: %s\n", argv[1], sitename);
434 return WERR_OK;
437 static WERROR cmd_netlogon_logon_ctrl(struct rpc_pipe_client *cli,
438 TALLOC_CTX *mem_ctx, int argc,
439 const char **argv)
441 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
442 WERROR werr;
443 const char *logon_server = cli->desthost;
444 enum netr_LogonControlCode function_code = 1;
445 uint32_t level = 1;
446 union netr_CONTROL_QUERY_INFORMATION info;
447 struct dcerpc_binding_handle *b = cli->binding_handle;
449 if (argc > 4) {
450 fprintf(stderr, "Usage: %s <logon_server> <function_code> "
451 "<level>\n", argv[0]);
452 return WERR_OK;
455 if (argc >= 2) {
456 logon_server = argv[1];
459 if (argc >= 3) {
460 function_code = atoi(argv[2]);
463 if (argc >= 4) {
464 level = atoi(argv[3]);
467 status = dcerpc_netr_LogonControl(b, mem_ctx,
468 logon_server,
469 function_code,
470 level,
471 &info,
472 &werr);
473 if (!NT_STATUS_IS_OK(status)) {
474 return ntstatus_to_werror(status);
477 if (!W_ERROR_IS_OK(werr)) {
478 return werr;
481 /* Display results */
483 return werr;
486 /* Display sam synchronisation information */
488 static void display_sam_sync(struct netr_DELTA_ENUM_ARRAY *r)
490 uint32_t i, j;
492 for (i=0; i < r->num_deltas; i++) {
494 union netr_DELTA_UNION u = r->delta_enum[i].delta_union;
495 union netr_DELTA_ID_UNION id = r->delta_enum[i].delta_id_union;
497 switch (r->delta_enum[i].delta_type) {
498 case NETR_DELTA_DOMAIN:
499 printf("Domain: %s\n",
500 u.domain->domain_name.string);
501 break;
502 case NETR_DELTA_GROUP:
503 printf("Group: %s\n",
504 u.group->group_name.string);
505 break;
506 case NETR_DELTA_DELETE_GROUP:
507 printf("Delete Group: %d\n",
508 id.rid);
509 break;
510 case NETR_DELTA_RENAME_GROUP:
511 printf("Rename Group: %s -> %s\n",
512 u.rename_group->OldName.string,
513 u.rename_group->NewName.string);
514 break;
515 case NETR_DELTA_USER:
516 printf("Account: %s\n",
517 u.user->account_name.string);
518 break;
519 case NETR_DELTA_DELETE_USER:
520 printf("Delete User: %d\n",
521 id.rid);
522 break;
523 case NETR_DELTA_RENAME_USER:
524 printf("Rename user: %s -> %s\n",
525 u.rename_user->OldName.string,
526 u.rename_user->NewName.string);
527 break;
528 case NETR_DELTA_GROUP_MEMBER:
529 for (j=0; j < u.group_member->num_rids; j++) {
530 printf("rid 0x%x, attrib 0x%08x\n",
531 u.group_member->rids[j],
532 u.group_member->attribs[j]);
534 break;
535 case NETR_DELTA_ALIAS:
536 printf("Alias: %s\n",
537 u.alias->alias_name.string);
538 break;
539 case NETR_DELTA_DELETE_ALIAS:
540 printf("Delete Alias: %d\n",
541 id.rid);
542 break;
543 case NETR_DELTA_RENAME_ALIAS:
544 printf("Rename alias: %s -> %s\n",
545 u.rename_alias->OldName.string,
546 u.rename_alias->NewName.string);
547 break;
548 case NETR_DELTA_ALIAS_MEMBER:
549 for (j=0; j < u.alias_member->sids.num_sids; j++) {
550 fstring sid_str;
551 sid_to_fstring(sid_str,
552 u.alias_member->sids.sids[j].sid);
553 printf("%s\n", sid_str);
555 break;
556 case NETR_DELTA_POLICY:
557 printf("Policy: %s\n",
558 sid_string_dbg(id.sid));
559 break;
560 case NETR_DELTA_TRUSTED_DOMAIN:
561 printf("Trusted Domain: %s\n",
562 u.trusted_domain->domain_name.string);
563 break;
564 case NETR_DELTA_DELETE_TRUST:
565 printf("Delete Trust: %s\n",
566 sid_string_dbg(id.sid));
567 break;
568 case NETR_DELTA_ACCOUNT:
569 printf("Account: %s\n",
570 sid_string_dbg(id.sid));
571 break;
572 case NETR_DELTA_DELETE_ACCOUNT:
573 printf("Delete Account: %s\n",
574 sid_string_dbg(id.sid));
575 break;
576 case NETR_DELTA_SECRET:
577 printf("Secret: %s\n",
578 id.name);
579 break;
580 case NETR_DELTA_DELETE_SECRET:
581 printf("Delete Secret: %s\n",
582 id.name);
583 break;
584 case NETR_DELTA_DELETE_GROUP2:
585 printf("Delete Group2: %s\n",
586 u.delete_group->account_name);
587 break;
588 case NETR_DELTA_DELETE_USER2:
589 printf("Delete User2: %s\n",
590 u.delete_user->account_name);
591 break;
592 case NETR_DELTA_MODIFY_COUNT:
593 printf("sam sequence update: 0x%016llx\n",
594 (unsigned long long) *u.modified_count);
595 break;
596 default:
597 printf("unknown delta type 0x%02x\n",
598 r->delta_enum[i].delta_type);
599 break;
604 /* Perform sam synchronisation */
606 static NTSTATUS cmd_netlogon_sam_sync(struct rpc_pipe_client *cli,
607 TALLOC_CTX *mem_ctx, int argc,
608 const char **argv)
610 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
611 NTSTATUS status;
612 const char *logon_server = cli->desthost;
613 const char *computername = lp_netbios_name();
614 struct netr_Authenticator credential;
615 struct netr_Authenticator return_authenticator;
616 enum netr_SamDatabaseID database_id = SAM_DATABASE_DOMAIN;
617 uint16_t restart_state = 0;
618 uint32_t sync_context = 0;
619 struct dcerpc_binding_handle *b = cli->binding_handle;
621 if (argc > 2) {
622 fprintf(stderr, "Usage: %s [database_id]\n", argv[0]);
623 return NT_STATUS_OK;
626 if (argc == 2) {
627 database_id = atoi(argv[1]);
630 /* Synchronise sam database */
632 do {
633 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
634 struct netlogon_creds_CredentialState *creds = NULL;
636 if (rpcclient_netlogon_creds == NULL) {
637 return NT_STATUS_UNSUCCESSFUL;
640 status = netlogon_creds_cli_lock(rpcclient_netlogon_creds,
641 mem_ctx, &creds);
642 if (!NT_STATUS_IS_OK(status)) {
643 return status;
646 netlogon_creds_client_authenticator(creds, &credential);
648 status = dcerpc_netr_DatabaseSync2(b, mem_ctx,
649 logon_server,
650 computername,
651 &credential,
652 &return_authenticator,
653 database_id,
654 restart_state,
655 &sync_context,
656 &delta_enum_array,
657 0xffff,
658 &result);
659 if (!NT_STATUS_IS_OK(status)) {
660 TALLOC_FREE(creds);
661 return status;
664 /* Check returned credentials. */
665 if (!netlogon_creds_client_check(creds,
666 &return_authenticator.cred)) {
667 DEBUG(0,("credentials chain check failed\n"));
668 TALLOC_FREE(creds);
669 return NT_STATUS_ACCESS_DENIED;
671 TALLOC_FREE(creds);
673 if (NT_STATUS_IS_ERR(result)) {
674 break;
677 /* Display results */
679 display_sam_sync(delta_enum_array);
681 TALLOC_FREE(delta_enum_array);
683 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
685 return result;
688 /* Perform sam delta synchronisation */
690 static NTSTATUS cmd_netlogon_sam_deltas(struct rpc_pipe_client *cli,
691 TALLOC_CTX *mem_ctx, int argc,
692 const char **argv)
694 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
695 NTSTATUS status;
696 uint32_t tmp;
697 const char *logon_server = cli->desthost;
698 const char *computername = lp_netbios_name();
699 struct netr_Authenticator credential;
700 struct netr_Authenticator return_authenticator;
701 enum netr_SamDatabaseID database_id = SAM_DATABASE_DOMAIN;
702 uint64_t sequence_num;
703 struct dcerpc_binding_handle *b = cli->binding_handle;
705 if (argc != 3) {
706 fprintf(stderr, "Usage: %s database_id seqnum\n", argv[0]);
707 return NT_STATUS_OK;
710 database_id = atoi(argv[1]);
711 tmp = atoi(argv[2]);
713 sequence_num = tmp & 0xffff;
715 do {
716 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
717 struct netlogon_creds_CredentialState *creds = NULL;
719 if (rpcclient_netlogon_creds == NULL) {
720 return NT_STATUS_UNSUCCESSFUL;
723 status = netlogon_creds_cli_lock(rpcclient_netlogon_creds,
724 mem_ctx, &creds);
725 if (!NT_STATUS_IS_OK(status)) {
726 return status;
729 netlogon_creds_client_authenticator(creds, &credential);
731 status = dcerpc_netr_DatabaseDeltas(b, mem_ctx,
732 logon_server,
733 computername,
734 &credential,
735 &return_authenticator,
736 database_id,
737 &sequence_num,
738 &delta_enum_array,
739 0xffff,
740 &result);
741 if (!NT_STATUS_IS_OK(status)) {
742 TALLOC_FREE(creds);
743 return status;
746 /* Check returned credentials. */
747 if (!netlogon_creds_client_check(creds,
748 &return_authenticator.cred)) {
749 DEBUG(0,("credentials chain check failed\n"));
750 TALLOC_FREE(creds);
751 return NT_STATUS_ACCESS_DENIED;
753 TALLOC_FREE(creds);
755 if (NT_STATUS_IS_ERR(result)) {
756 break;
759 /* Display results */
761 display_sam_sync(delta_enum_array);
763 TALLOC_FREE(delta_enum_array);
765 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
767 return result;
770 /* Log on a domain user */
772 static NTSTATUS cmd_netlogon_sam_logon(struct rpc_pipe_client *cli,
773 TALLOC_CTX *mem_ctx, int argc,
774 const char **argv)
776 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
777 int logon_type = NetlogonNetworkInformation;
778 const char *username, *password;
779 uint32 logon_param = 0;
780 const char *workstation = NULL;
781 struct netr_SamInfo3 *info3 = NULL;
783 /* Check arguments */
785 if (argc < 3 || argc > 6) {
786 fprintf(stderr, "Usage: samlogon <username> <password> [workstation]"
787 "[logon_type (1 or 2)] [logon_parameter]\n");
788 return NT_STATUS_OK;
791 username = argv[1];
792 password = argv[2];
794 if (argc >= 4)
795 workstation = argv[3];
797 if (argc >= 5)
798 sscanf(argv[4], "%i", &logon_type);
800 if (argc == 6)
801 sscanf(argv[5], "%x", &logon_param);
803 /* Perform the sam logon */
805 result = rpccli_netlogon_password_logon(rpcclient_netlogon_creds,
806 cli->binding_handle,
807 mem_ctx,
808 logon_param,
809 lp_workgroup(),
810 username,
811 password,
812 workstation,
813 logon_type,
814 &info3);
815 if (!NT_STATUS_IS_OK(result))
816 goto done;
818 done:
819 return result;
822 /* Change the trust account password */
824 static NTSTATUS cmd_netlogon_change_trust_pw(struct rpc_pipe_client *cli,
825 TALLOC_CTX *mem_ctx, int argc,
826 const char **argv)
828 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
830 /* Check arguments */
832 if (argc > 1) {
833 fprintf(stderr, "Usage: change_trust_pw");
834 return NT_STATUS_OK;
837 result = trust_pw_change(rpcclient_netlogon_creds,
838 rpcclient_msg_ctx,
839 cli->binding_handle,
840 lp_workgroup(),
841 true); /* force */
842 if (!NT_STATUS_IS_OK(result))
843 goto done;
845 done:
846 return result;
849 static WERROR cmd_netlogon_gettrustrid(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_name = lp_workgroup();
857 uint32_t rid = 0;
858 struct dcerpc_binding_handle *b = cli->binding_handle;
860 if (argc < 1 || argc > 3) {
861 fprintf(stderr, "Usage: %s <server_name> <domain_name>\n",
862 argv[0]);
863 return WERR_OK;
866 if (argc >= 2) {
867 server_name = argv[1];
870 if (argc >= 3) {
871 domain_name = argv[2];
874 status = dcerpc_netr_LogonGetTrustRid(b, mem_ctx,
875 server_name,
876 domain_name,
877 &rid,
878 &werr);
879 if (!NT_STATUS_IS_OK(status)) {
880 werr = ntstatus_to_werror(status);
881 goto done;
884 if (W_ERROR_IS_OK(werr)) {
885 printf("Rid: %d\n", rid);
887 done:
888 return werr;
891 static WERROR cmd_netlogon_dsr_enumtrustdom(struct rpc_pipe_client *cli,
892 TALLOC_CTX *mem_ctx, int argc,
893 const char **argv)
895 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
896 WERROR werr = WERR_GENERAL_FAILURE;
897 const char *server_name = cli->desthost;
898 uint32_t trust_flags = NETR_TRUST_FLAG_IN_FOREST;
899 struct netr_DomainTrustList trusts;
900 struct dcerpc_binding_handle *b = cli->binding_handle;
902 if (argc < 1 || argc > 3) {
903 fprintf(stderr, "Usage: %s <server_name> <trust_flags>\n",
904 argv[0]);
905 return WERR_OK;
908 if (argc >= 2) {
909 server_name = argv[1];
912 if (argc >= 3) {
913 sscanf(argv[2], "%x", &trust_flags);
916 status = dcerpc_netr_DsrEnumerateDomainTrusts(b, mem_ctx,
917 server_name,
918 trust_flags,
919 &trusts,
920 &werr);
921 if (!NT_STATUS_IS_OK(status)) {
922 werr = ntstatus_to_werror(status);
923 goto done;
926 if (W_ERROR_IS_OK(werr)) {
927 int i;
929 printf("%d domains returned\n", trusts.count);
931 for (i=0; i<trusts.count; i++ ) {
932 printf("%s (%s)\n",
933 trusts.array[i].dns_name,
934 trusts.array[i].netbios_name);
937 done:
938 return werr;
941 static WERROR cmd_netlogon_deregisterdnsrecords(struct rpc_pipe_client *cli,
942 TALLOC_CTX *mem_ctx, int argc,
943 const char **argv)
945 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
946 WERROR werr = WERR_GENERAL_FAILURE;
947 const char *server_name = cli->desthost;
948 const char *domain = lp_workgroup();
949 const char *dns_host = NULL;
950 struct dcerpc_binding_handle *b = cli->binding_handle;
952 if (argc < 1 || argc > 4) {
953 fprintf(stderr, "Usage: %s <server_name> <domain_name> "
954 "<dns_host>\n", argv[0]);
955 return WERR_OK;
958 if (argc >= 2) {
959 server_name = argv[1];
962 if (argc >= 3) {
963 domain = argv[2];
966 if (argc >= 4) {
967 dns_host = argv[3];
970 status = dcerpc_netr_DsrDeregisterDNSHostRecords(b, mem_ctx,
971 server_name,
972 domain,
973 NULL,
974 NULL,
975 dns_host,
976 &werr);
977 if (!NT_STATUS_IS_OK(status)) {
978 werr = ntstatus_to_werror(status);
979 goto done;
982 if (W_ERROR_IS_OK(werr)) {
983 printf("success\n");
985 done:
986 return werr;
989 static WERROR cmd_netlogon_dsr_getforesttrustinfo(struct rpc_pipe_client *cli,
990 TALLOC_CTX *mem_ctx, int argc,
991 const char **argv)
993 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
994 WERROR werr = WERR_GENERAL_FAILURE;
995 const char *server_name = cli->desthost;
996 const char *trusted_domain_name = NULL;
997 struct lsa_ForestTrustInformation *info = NULL;
998 uint32_t flags = 0;
999 struct dcerpc_binding_handle *b = cli->binding_handle;
1001 if (argc < 1 || argc > 4) {
1002 fprintf(stderr, "Usage: %s <server_name> <trusted_domain_name> "
1003 "<flags>\n", argv[0]);
1004 return WERR_OK;
1007 if (argc >= 2) {
1008 server_name = argv[1];
1011 if (argc >= 3) {
1012 trusted_domain_name = argv[2];
1015 if (argc >= 4) {
1016 sscanf(argv[3], "%x", &flags);
1019 status = dcerpc_netr_DsRGetForestTrustInformation(b, mem_ctx,
1020 server_name,
1021 trusted_domain_name,
1022 flags,
1023 &info,
1024 &werr);
1025 if (!NT_STATUS_IS_OK(status)) {
1026 werr = ntstatus_to_werror(status);
1027 goto done;
1030 if (W_ERROR_IS_OK(werr)) {
1031 printf("success\n");
1033 done:
1034 return werr;
1037 static NTSTATUS cmd_netlogon_enumtrusteddomains(struct rpc_pipe_client *cli,
1038 TALLOC_CTX *mem_ctx, int argc,
1039 const char **argv)
1041 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1042 NTSTATUS result;
1043 const char *server_name = cli->desthost;
1044 struct netr_Blob blob;
1045 struct dcerpc_binding_handle *b = cli->binding_handle;
1048 if (argc < 1 || argc > 3) {
1049 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
1050 return NT_STATUS_OK;
1053 if (argc >= 2) {
1054 server_name = argv[1];
1057 status = dcerpc_netr_NetrEnumerateTrustedDomains(b, mem_ctx,
1058 server_name,
1059 &blob,
1060 &result);
1061 if (!NT_STATUS_IS_OK(status)) {
1062 goto done;
1065 if (!NT_STATUS_IS_OK(result)) {
1066 status = result;
1067 goto done;
1070 printf("success\n");
1071 dump_data(1, blob.data, blob.length);
1072 done:
1073 return status;
1076 static WERROR cmd_netlogon_enumtrusteddomainsex(struct rpc_pipe_client *cli,
1077 TALLOC_CTX *mem_ctx, int argc,
1078 const char **argv)
1080 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1081 WERROR werr = WERR_GENERAL_FAILURE;
1082 const char *server_name = cli->desthost;
1083 struct netr_DomainTrustList list;
1084 struct dcerpc_binding_handle *b = cli->binding_handle;
1086 if (argc < 1 || argc > 3) {
1087 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
1088 return WERR_OK;
1091 if (argc >= 2) {
1092 server_name = argv[1];
1095 status = dcerpc_netr_NetrEnumerateTrustedDomainsEx(b, mem_ctx,
1096 server_name,
1097 &list,
1098 &werr);
1099 if (!NT_STATUS_IS_OK(status)) {
1100 werr = ntstatus_to_werror(status);
1101 goto done;
1104 if (W_ERROR_IS_OK(werr)) {
1105 printf("success\n");
1107 done:
1108 return werr;
1111 static WERROR cmd_netlogon_getdcsitecoverage(struct rpc_pipe_client *cli,
1112 TALLOC_CTX *mem_ctx, int argc,
1113 const char **argv)
1115 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1116 WERROR werr = WERR_GENERAL_FAILURE;
1117 const char *server_name = cli->desthost;
1118 struct DcSitesCtr *ctr = NULL;
1119 struct dcerpc_binding_handle *b = cli->binding_handle;
1121 if (argc < 1 || argc > 3) {
1122 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
1123 return WERR_OK;
1126 if (argc >= 2) {
1127 server_name = argv[1];
1130 status = dcerpc_netr_DsrGetDcSiteCoverageW(b, mem_ctx,
1131 server_name,
1132 &ctr,
1133 &werr);
1134 if (!NT_STATUS_IS_OK(status)) {
1135 werr = ntstatus_to_werror(status);
1136 goto done;
1139 if (W_ERROR_IS_OK(werr) && ctr->num_sites) {
1140 int i;
1141 printf("sites covered by this DC: %d\n", ctr->num_sites);
1142 for (i=0; i<ctr->num_sites; i++) {
1143 printf("%s\n", ctr->sites[i].string);
1146 done:
1147 return werr;
1150 static NTSTATUS cmd_netlogon_database_redo(struct rpc_pipe_client *cli,
1151 TALLOC_CTX *mem_ctx, int argc,
1152 const char **argv)
1154 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1155 NTSTATUS result;
1156 const char *server_name = cli->desthost;
1157 struct netr_Authenticator clnt_creds, srv_cred;
1158 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1159 struct netr_ChangeLogEntry e;
1160 uint32_t rid = 500;
1161 struct dcerpc_binding_handle *b = cli->binding_handle;
1162 struct netlogon_creds_CredentialState *creds = NULL;
1164 if (argc > 2) {
1165 fprintf(stderr, "Usage: %s <user rid>\n", argv[0]);
1166 return NT_STATUS_OK;
1169 if (argc == 2) {
1170 sscanf(argv[1], "%d", &rid);
1173 if (rpcclient_netlogon_creds == NULL) {
1174 return NT_STATUS_UNSUCCESSFUL;
1177 status = netlogon_creds_cli_lock(rpcclient_netlogon_creds,
1178 mem_ctx, &creds);
1179 if (!NT_STATUS_IS_OK(status)) {
1180 return status;
1183 netlogon_creds_client_authenticator(creds, &clnt_creds);
1185 ZERO_STRUCT(e);
1187 e.object_rid = rid;
1188 e.db_index = SAM_DATABASE_DOMAIN;
1189 e.delta_type = NETR_DELTA_USER;
1191 status = dcerpc_netr_DatabaseRedo(b, mem_ctx,
1192 server_name,
1193 lp_netbios_name(),
1194 &clnt_creds,
1195 &srv_cred,
1197 0, /* is calculated automatically */
1198 &delta_enum_array,
1199 &result);
1200 if (!NT_STATUS_IS_OK(status)) {
1201 TALLOC_FREE(creds);
1202 return status;
1205 if (!netlogon_creds_client_check(creds, &srv_cred.cred)) {
1206 DEBUG(0,("credentials chain check failed\n"));
1207 TALLOC_FREE(creds);
1208 return NT_STATUS_ACCESS_DENIED;
1210 TALLOC_FREE(creds);
1212 return result;
1215 static NTSTATUS cmd_netlogon_capabilities(struct rpc_pipe_client *cli,
1216 TALLOC_CTX *mem_ctx, int argc,
1217 const char **argv)
1219 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1220 NTSTATUS result;
1221 struct netr_Authenticator credential;
1222 struct netr_Authenticator return_authenticator;
1223 union netr_Capabilities capabilities;
1224 uint32_t level = 1;
1225 struct dcerpc_binding_handle *b = cli->binding_handle;
1226 struct netlogon_creds_CredentialState *creds = NULL;
1228 if (argc > 2) {
1229 fprintf(stderr, "Usage: %s <level>\n", argv[0]);
1230 return NT_STATUS_OK;
1233 if (argc == 2) {
1234 level = atoi(argv[1]);
1237 ZERO_STRUCT(return_authenticator);
1239 if (rpcclient_netlogon_creds == NULL) {
1240 return NT_STATUS_UNSUCCESSFUL;
1243 status = netlogon_creds_cli_lock(rpcclient_netlogon_creds,
1244 mem_ctx, &creds);
1245 if (!NT_STATUS_IS_OK(status)) {
1246 return status;
1249 netlogon_creds_client_authenticator(creds, &credential);
1251 status = dcerpc_netr_LogonGetCapabilities(b, mem_ctx,
1252 cli->desthost,
1253 lp_netbios_name(),
1254 &credential,
1255 &return_authenticator,
1256 level,
1257 &capabilities,
1258 &result);
1259 if (!NT_STATUS_IS_OK(status)) {
1260 TALLOC_FREE(creds);
1261 return status;
1264 if (!netlogon_creds_client_check(creds,
1265 &return_authenticator.cred)) {
1266 DEBUG(0,("credentials chain check failed\n"));
1267 TALLOC_FREE(creds);
1268 return NT_STATUS_ACCESS_DENIED;
1270 TALLOC_FREE(creds);
1272 printf("capabilities: 0x%08x\n", capabilities.server_capabilities);
1274 return result;
1277 /* List of commands exported by this module */
1279 struct cmd_set netlogon_commands[] = {
1281 { "NETLOGON" },
1283 { "logonctrl2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl2, &ndr_table_netlogon, NULL, "Logon Control 2", "" },
1284 { "getanydcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getanydcname, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1285 { "getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcname, &ndr_table_netlogon, NULL, "Get trusted PDC name", "" },
1286 { "dsr_getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcname, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1287 { "dsr_getdcnameex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1288 { "dsr_getdcnameex2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex2, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1289 { "dsr_getsitename", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getsitename, &ndr_table_netlogon, NULL, "Get sitename", "" },
1290 { "dsr_getforesttrustinfo", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getforesttrustinfo, &ndr_table_netlogon, NULL, "Get Forest Trust Info", "" },
1291 { "logonctrl", RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl, &ndr_table_netlogon, NULL, "Logon Control", "" },
1292 { "samsync", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_sync, NULL, &ndr_table_netlogon, NULL, "Sam Synchronisation", "" },
1293 { "samdeltas", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_deltas, NULL, &ndr_table_netlogon, NULL, "Query Sam Deltas", "" },
1294 { "samlogon", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_logon, NULL, &ndr_table_netlogon, NULL, "Sam Logon", "" },
1295 { "change_trust_pw", RPC_RTYPE_NTSTATUS, cmd_netlogon_change_trust_pw, NULL, &ndr_table_netlogon, NULL, "Change Trust Account Password", "" },
1296 { "gettrustrid", RPC_RTYPE_WERROR, NULL, cmd_netlogon_gettrustrid, &ndr_table_netlogon, NULL, "Get trust rid", "" },
1297 { "dsr_enumtrustdom", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon, NULL, "Enumerate trusted domains", "" },
1298 { "dsenumdomtrusts", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon, NULL, "Enumerate all trusted domains in an AD forest", "" },
1299 { "deregisterdnsrecords", RPC_RTYPE_WERROR, NULL, cmd_netlogon_deregisterdnsrecords, &ndr_table_netlogon, NULL, "Deregister DNS records", "" },
1300 { "netrenumtrusteddomains", RPC_RTYPE_NTSTATUS, cmd_netlogon_enumtrusteddomains, NULL, &ndr_table_netlogon, NULL, "Enumerate trusted domains", "" },
1301 { "netrenumtrusteddomainsex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_enumtrusteddomainsex, &ndr_table_netlogon, NULL, "Enumerate trusted domains", "" },
1302 { "getdcsitecoverage", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcsitecoverage, &ndr_table_netlogon, NULL, "Get the Site-Coverage from a DC", "" },
1303 { "database_redo", RPC_RTYPE_NTSTATUS, cmd_netlogon_database_redo, NULL, &ndr_table_netlogon, NULL, "Replicate single object from a DC", "" },
1304 { "capabilities", RPC_RTYPE_NTSTATUS, cmd_netlogon_capabilities, NULL, &ndr_table_netlogon, NULL, "Return Capabilities", "" },
1306 { NULL }