WHATSNEW: Add release notes for Samba 4.6.13.
[Samba.git] / source3 / rpcclient / cmd_netlogon.c
blob7d12f17943a74b50b157c41d03165ad5d7c0aa96
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_t 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_NOT_ENOUGH_MEMORY;
239 if (argc >= 4) {
240 if (!NT_STATUS_IS_OK(GUID_from_string(argv[3], &site_guid))) {
241 return WERR_NOT_ENOUGH_MEMORY;
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_NOT_ENOUGH_MEMORY;
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_NOT_ENOUGH_MEMORY;
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_t 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 if (rpcclient_netlogon_creds == NULL) {
804 result = NT_STATUS_UNSUCCESSFUL;
805 goto done;
808 /* Perform the sam logon */
810 result = rpccli_netlogon_password_logon(rpcclient_netlogon_creds,
811 cli->binding_handle,
812 mem_ctx,
813 logon_param,
814 lp_workgroup(),
815 username,
816 password,
817 workstation,
818 logon_type,
819 &info3);
820 if (!NT_STATUS_IS_OK(result))
821 goto done;
823 done:
824 return result;
827 /* Change the trust account password */
829 static NTSTATUS cmd_netlogon_change_trust_pw(struct rpc_pipe_client *cli,
830 TALLOC_CTX *mem_ctx, int argc,
831 const char **argv)
833 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
834 const char *dcname = cli->desthost;
836 /* Check arguments */
838 if (argc > 1) {
839 fprintf(stderr, "Usage: change_trust_pw");
840 return NT_STATUS_OK;
843 result = trust_pw_change(rpcclient_netlogon_creds,
844 rpcclient_msg_ctx,
845 cli->binding_handle,
846 lp_workgroup(),
847 dcname,
848 true); /* force */
849 if (!NT_STATUS_IS_OK(result))
850 goto done;
852 done:
853 return result;
856 static WERROR cmd_netlogon_gettrustrid(struct rpc_pipe_client *cli,
857 TALLOC_CTX *mem_ctx, int argc,
858 const char **argv)
860 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
861 WERROR werr = WERR_GEN_FAILURE;
862 const char *server_name = cli->desthost;
863 const char *domain_name = lp_workgroup();
864 uint32_t rid = 0;
865 struct dcerpc_binding_handle *b = cli->binding_handle;
867 if (argc < 1 || argc > 3) {
868 fprintf(stderr, "Usage: %s <server_name> <domain_name>\n",
869 argv[0]);
870 return WERR_OK;
873 if (argc >= 2) {
874 server_name = argv[1];
877 if (argc >= 3) {
878 domain_name = argv[2];
881 status = dcerpc_netr_LogonGetTrustRid(b, mem_ctx,
882 server_name,
883 domain_name,
884 &rid,
885 &werr);
886 if (!NT_STATUS_IS_OK(status)) {
887 werr = ntstatus_to_werror(status);
888 goto done;
891 if (W_ERROR_IS_OK(werr)) {
892 printf("Rid: %d\n", rid);
894 done:
895 return werr;
898 static WERROR cmd_netlogon_dsr_enumtrustdom(struct rpc_pipe_client *cli,
899 TALLOC_CTX *mem_ctx, int argc,
900 const char **argv)
902 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
903 WERROR werr = WERR_GEN_FAILURE;
904 const char *server_name = cli->desthost;
905 uint32_t trust_flags = NETR_TRUST_FLAG_IN_FOREST;
906 struct netr_DomainTrustList trusts;
907 struct dcerpc_binding_handle *b = cli->binding_handle;
909 if (argc < 1 || argc > 3) {
910 fprintf(stderr, "Usage: %s <server_name> <trust_flags>\n",
911 argv[0]);
912 return WERR_OK;
915 if (argc >= 2) {
916 server_name = argv[1];
919 if (argc >= 3) {
920 sscanf(argv[2], "%x", &trust_flags);
923 status = dcerpc_netr_DsrEnumerateDomainTrusts(b, mem_ctx,
924 server_name,
925 trust_flags,
926 &trusts,
927 &werr);
928 if (!NT_STATUS_IS_OK(status)) {
929 werr = ntstatus_to_werror(status);
930 goto done;
933 if (W_ERROR_IS_OK(werr)) {
934 int i;
936 printf("%d domains returned\n", trusts.count);
938 for (i=0; i<trusts.count; i++ ) {
939 printf("%s (%s)\n",
940 trusts.array[i].dns_name,
941 trusts.array[i].netbios_name);
944 done:
945 return werr;
948 static WERROR cmd_netlogon_deregisterdnsrecords(struct rpc_pipe_client *cli,
949 TALLOC_CTX *mem_ctx, int argc,
950 const char **argv)
952 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
953 WERROR werr = WERR_GEN_FAILURE;
954 const char *server_name = cli->desthost;
955 const char *domain = lp_workgroup();
956 const char *dns_host = NULL;
957 struct dcerpc_binding_handle *b = cli->binding_handle;
959 if (argc < 1 || argc > 4) {
960 fprintf(stderr, "Usage: %s <server_name> <domain_name> "
961 "<dns_host>\n", argv[0]);
962 return WERR_OK;
965 if (argc >= 2) {
966 server_name = argv[1];
969 if (argc >= 3) {
970 domain = argv[2];
973 if (argc >= 4) {
974 dns_host = argv[3];
977 status = dcerpc_netr_DsrDeregisterDNSHostRecords(b, mem_ctx,
978 server_name,
979 domain,
980 NULL,
981 NULL,
982 dns_host,
983 &werr);
984 if (!NT_STATUS_IS_OK(status)) {
985 werr = ntstatus_to_werror(status);
986 goto done;
989 if (W_ERROR_IS_OK(werr)) {
990 printf("success\n");
992 done:
993 return werr;
996 static WERROR cmd_netlogon_dsr_getforesttrustinfo(struct rpc_pipe_client *cli,
997 TALLOC_CTX *mem_ctx, int argc,
998 const char **argv)
1000 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1001 WERROR werr = WERR_GEN_FAILURE;
1002 const char *server_name = cli->desthost;
1003 const char *trusted_domain_name = NULL;
1004 struct lsa_ForestTrustInformation *info = NULL;
1005 uint32_t flags = 0;
1006 struct dcerpc_binding_handle *b = cli->binding_handle;
1008 if (argc < 1 || argc > 4) {
1009 fprintf(stderr, "Usage: %s <server_name> <trusted_domain_name> "
1010 "<flags>\n", argv[0]);
1011 return WERR_OK;
1014 if (argc >= 2) {
1015 server_name = argv[1];
1018 if (argc >= 3) {
1019 trusted_domain_name = argv[2];
1022 if (argc >= 4) {
1023 sscanf(argv[3], "%x", &flags);
1026 status = dcerpc_netr_DsRGetForestTrustInformation(b, mem_ctx,
1027 server_name,
1028 trusted_domain_name,
1029 flags,
1030 &info,
1031 &werr);
1032 if (!NT_STATUS_IS_OK(status)) {
1033 werr = ntstatus_to_werror(status);
1034 goto done;
1037 if (W_ERROR_IS_OK(werr)) {
1038 printf("success\n");
1040 done:
1041 return werr;
1044 static NTSTATUS cmd_netlogon_enumtrusteddomains(struct rpc_pipe_client *cli,
1045 TALLOC_CTX *mem_ctx, int argc,
1046 const char **argv)
1048 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1049 NTSTATUS result;
1050 const char *server_name = cli->desthost;
1051 struct netr_Blob blob;
1052 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 NT_STATUS_OK;
1060 if (argc >= 2) {
1061 server_name = argv[1];
1064 status = dcerpc_netr_NetrEnumerateTrustedDomains(b, mem_ctx,
1065 server_name,
1066 &blob,
1067 &result);
1068 if (!NT_STATUS_IS_OK(status)) {
1069 goto done;
1072 if (!NT_STATUS_IS_OK(result)) {
1073 status = result;
1074 goto done;
1077 printf("success\n");
1078 dump_data(1, blob.data, blob.length);
1079 done:
1080 return status;
1083 static WERROR cmd_netlogon_enumtrusteddomainsex(struct rpc_pipe_client *cli,
1084 TALLOC_CTX *mem_ctx, int argc,
1085 const char **argv)
1087 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1088 WERROR werr = WERR_GEN_FAILURE;
1089 const char *server_name = cli->desthost;
1090 struct netr_DomainTrustList list;
1091 struct dcerpc_binding_handle *b = cli->binding_handle;
1093 if (argc < 1 || argc > 3) {
1094 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
1095 return WERR_OK;
1098 if (argc >= 2) {
1099 server_name = argv[1];
1102 status = dcerpc_netr_NetrEnumerateTrustedDomainsEx(b, mem_ctx,
1103 server_name,
1104 &list,
1105 &werr);
1106 if (!NT_STATUS_IS_OK(status)) {
1107 werr = ntstatus_to_werror(status);
1108 goto done;
1111 if (W_ERROR_IS_OK(werr)) {
1112 printf("success\n");
1114 done:
1115 return werr;
1118 static WERROR cmd_netlogon_getdcsitecoverage(struct rpc_pipe_client *cli,
1119 TALLOC_CTX *mem_ctx, int argc,
1120 const char **argv)
1122 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1123 WERROR werr = WERR_GEN_FAILURE;
1124 const char *server_name = cli->desthost;
1125 struct DcSitesCtr *ctr = NULL;
1126 struct dcerpc_binding_handle *b = cli->binding_handle;
1128 if (argc < 1 || argc > 3) {
1129 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
1130 return WERR_OK;
1133 if (argc >= 2) {
1134 server_name = argv[1];
1137 status = dcerpc_netr_DsrGetDcSiteCoverageW(b, mem_ctx,
1138 server_name,
1139 &ctr,
1140 &werr);
1141 if (!NT_STATUS_IS_OK(status)) {
1142 werr = ntstatus_to_werror(status);
1143 goto done;
1146 if (W_ERROR_IS_OK(werr) && ctr->num_sites) {
1147 int i;
1148 printf("sites covered by this DC: %d\n", ctr->num_sites);
1149 for (i=0; i<ctr->num_sites; i++) {
1150 printf("%s\n", ctr->sites[i].string);
1153 done:
1154 return werr;
1157 static NTSTATUS cmd_netlogon_database_redo(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 const char *server_name = cli->desthost;
1164 struct netr_Authenticator clnt_creds, srv_cred;
1165 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1166 struct netr_ChangeLogEntry e;
1167 uint32_t rid = 500;
1168 struct dcerpc_binding_handle *b = cli->binding_handle;
1169 struct netlogon_creds_CredentialState *creds = NULL;
1171 if (argc > 2) {
1172 fprintf(stderr, "Usage: %s <user rid>\n", argv[0]);
1173 return NT_STATUS_OK;
1176 if (argc == 2) {
1177 sscanf(argv[1], "%d", &rid);
1180 if (rpcclient_netlogon_creds == NULL) {
1181 return NT_STATUS_UNSUCCESSFUL;
1184 status = netlogon_creds_cli_lock(rpcclient_netlogon_creds,
1185 mem_ctx, &creds);
1186 if (!NT_STATUS_IS_OK(status)) {
1187 return status;
1190 netlogon_creds_client_authenticator(creds, &clnt_creds);
1192 ZERO_STRUCT(e);
1194 e.object_rid = rid;
1195 e.db_index = SAM_DATABASE_DOMAIN;
1196 e.delta_type = NETR_DELTA_USER;
1198 status = dcerpc_netr_DatabaseRedo(b, mem_ctx,
1199 server_name,
1200 lp_netbios_name(),
1201 &clnt_creds,
1202 &srv_cred,
1204 0, /* is calculated automatically */
1205 &delta_enum_array,
1206 &result);
1207 if (!NT_STATUS_IS_OK(status)) {
1208 TALLOC_FREE(creds);
1209 return status;
1212 if (!netlogon_creds_client_check(creds, &srv_cred.cred)) {
1213 DEBUG(0,("credentials chain check failed\n"));
1214 TALLOC_FREE(creds);
1215 return NT_STATUS_ACCESS_DENIED;
1217 TALLOC_FREE(creds);
1219 return result;
1222 static NTSTATUS cmd_netlogon_capabilities(struct rpc_pipe_client *cli,
1223 TALLOC_CTX *mem_ctx, int argc,
1224 const char **argv)
1226 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1227 NTSTATUS result;
1228 struct netr_Authenticator credential;
1229 struct netr_Authenticator return_authenticator;
1230 union netr_Capabilities capabilities;
1231 uint32_t level = 1;
1232 struct dcerpc_binding_handle *b = cli->binding_handle;
1233 struct netlogon_creds_CredentialState *creds = NULL;
1235 if (argc > 2) {
1236 fprintf(stderr, "Usage: %s <level>\n", argv[0]);
1237 return NT_STATUS_OK;
1240 if (argc == 2) {
1241 level = atoi(argv[1]);
1244 ZERO_STRUCT(return_authenticator);
1246 if (rpcclient_netlogon_creds == NULL) {
1247 return NT_STATUS_UNSUCCESSFUL;
1250 status = netlogon_creds_cli_lock(rpcclient_netlogon_creds,
1251 mem_ctx, &creds);
1252 if (!NT_STATUS_IS_OK(status)) {
1253 return status;
1256 netlogon_creds_client_authenticator(creds, &credential);
1258 status = dcerpc_netr_LogonGetCapabilities(b, mem_ctx,
1259 cli->desthost,
1260 lp_netbios_name(),
1261 &credential,
1262 &return_authenticator,
1263 level,
1264 &capabilities,
1265 &result);
1266 if (!NT_STATUS_IS_OK(status)) {
1267 TALLOC_FREE(creds);
1268 return status;
1271 if (!netlogon_creds_client_check(creds,
1272 &return_authenticator.cred)) {
1273 DEBUG(0,("credentials chain check failed\n"));
1274 TALLOC_FREE(creds);
1275 return NT_STATUS_ACCESS_DENIED;
1277 TALLOC_FREE(creds);
1279 printf("capabilities: 0x%08x\n", capabilities.server_capabilities);
1281 return result;
1284 /* List of commands exported by this module */
1286 struct cmd_set netlogon_commands[] = {
1288 { "NETLOGON" },
1290 { "logonctrl2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl2, &ndr_table_netlogon, NULL, "Logon Control 2", "" },
1291 { "getanydcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getanydcname, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1292 { "getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcname, &ndr_table_netlogon, NULL, "Get trusted PDC name", "" },
1293 { "dsr_getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcname, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1294 { "dsr_getdcnameex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1295 { "dsr_getdcnameex2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex2, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1296 { "dsr_getsitename", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getsitename, &ndr_table_netlogon, NULL, "Get sitename", "" },
1297 { "dsr_getforesttrustinfo", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getforesttrustinfo, &ndr_table_netlogon, NULL, "Get Forest Trust Info", "" },
1298 { "logonctrl", RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl, &ndr_table_netlogon, NULL, "Logon Control", "" },
1299 { "samsync", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_sync, NULL, &ndr_table_netlogon, NULL, "Sam Synchronisation", "", .use_netlogon_creds = true, },
1300 { "samdeltas", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_deltas, NULL, &ndr_table_netlogon, NULL, "Query Sam Deltas", "", .use_netlogon_creds = true, },
1301 { "samlogon", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_logon, NULL, &ndr_table_netlogon, NULL, "Sam Logon", "", .use_netlogon_creds = true, },
1302 { "change_trust_pw", RPC_RTYPE_NTSTATUS, cmd_netlogon_change_trust_pw, NULL, &ndr_table_netlogon, NULL, "Change Trust Account Password", "", .use_netlogon_creds = true, },
1303 { "gettrustrid", RPC_RTYPE_WERROR, NULL, cmd_netlogon_gettrustrid, &ndr_table_netlogon, NULL, "Get trust rid", "" },
1304 { "dsr_enumtrustdom", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon, NULL, "Enumerate trusted domains", "" },
1305 { "dsenumdomtrusts", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon, NULL, "Enumerate all trusted domains in an AD forest", "" },
1306 { "deregisterdnsrecords", RPC_RTYPE_WERROR, NULL, cmd_netlogon_deregisterdnsrecords, &ndr_table_netlogon, NULL, "Deregister DNS records", "" },
1307 { "netrenumtrusteddomains", RPC_RTYPE_NTSTATUS, cmd_netlogon_enumtrusteddomains, NULL, &ndr_table_netlogon, NULL, "Enumerate trusted domains", "" },
1308 { "netrenumtrusteddomainsex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_enumtrusteddomainsex, &ndr_table_netlogon, NULL, "Enumerate trusted domains", "" },
1309 { "getdcsitecoverage", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcsitecoverage, &ndr_table_netlogon, NULL, "Get the Site-Coverage from a DC", "" },
1310 { "database_redo", RPC_RTYPE_NTSTATUS, cmd_netlogon_database_redo, NULL, &ndr_table_netlogon, NULL, "Replicate single object from a DC", "", .use_netlogon_creds = true, },
1311 { "capabilities", RPC_RTYPE_NTSTATUS, cmd_netlogon_capabilities, NULL, &ndr_table_netlogon, NULL, "Return Capabilities", "", .use_netlogon_creds = true, },
1313 { NULL }