ctdb-eventscripts: Update comment in 11.routing
[Samba.git] / source3 / rpcclient / cmd_netlogon.c
blobb637b3e68c6f215958b36de304a7a861ba762946
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;
782 /* Check arguments */
784 if (argc < 3 || argc > 6) {
785 fprintf(stderr, "Usage: samlogon <username> <password> [workstation]"
786 "[logon_type (1 or 2)] [logon_parameter]\n");
787 return NT_STATUS_OK;
790 username = argv[1];
791 password = argv[2];
793 if (argc >= 4)
794 workstation = argv[3];
796 if (argc >= 5)
797 sscanf(argv[4], "%i", &logon_type);
799 if (argc == 6)
800 sscanf(argv[5], "%x", &logon_param);
802 /* Perform the sam logon */
804 result = rpccli_netlogon_password_logon(rpcclient_netlogon_creds,
805 cli->binding_handle,
806 logon_param,
807 lp_workgroup(),
808 username,
809 password,
810 workstation,
811 logon_type);
812 if (!NT_STATUS_IS_OK(result))
813 goto done;
815 done:
816 return result;
819 /* Change the trust account password */
821 static NTSTATUS cmd_netlogon_change_trust_pw(struct rpc_pipe_client *cli,
822 TALLOC_CTX *mem_ctx, int argc,
823 const char **argv)
825 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
827 /* Check arguments */
829 if (argc > 1) {
830 fprintf(stderr, "Usage: change_trust_pw");
831 return NT_STATUS_OK;
834 result = trust_pw_change(rpcclient_netlogon_creds,
835 rpcclient_msg_ctx,
836 cli->binding_handle,
837 lp_workgroup(),
838 true); /* force */
839 if (!NT_STATUS_IS_OK(result))
840 goto done;
842 done:
843 return result;
846 static WERROR cmd_netlogon_gettrustrid(struct rpc_pipe_client *cli,
847 TALLOC_CTX *mem_ctx, int argc,
848 const char **argv)
850 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
851 WERROR werr = WERR_GENERAL_FAILURE;
852 const char *server_name = cli->desthost;
853 const char *domain_name = lp_workgroup();
854 uint32_t rid = 0;
855 struct dcerpc_binding_handle *b = cli->binding_handle;
857 if (argc < 1 || argc > 3) {
858 fprintf(stderr, "Usage: %s <server_name> <domain_name>\n",
859 argv[0]);
860 return WERR_OK;
863 if (argc >= 2) {
864 server_name = argv[1];
867 if (argc >= 3) {
868 domain_name = argv[2];
871 status = dcerpc_netr_LogonGetTrustRid(b, mem_ctx,
872 server_name,
873 domain_name,
874 &rid,
875 &werr);
876 if (!NT_STATUS_IS_OK(status)) {
877 werr = ntstatus_to_werror(status);
878 goto done;
881 if (W_ERROR_IS_OK(werr)) {
882 printf("Rid: %d\n", rid);
884 done:
885 return werr;
888 static WERROR cmd_netlogon_dsr_enumtrustdom(struct rpc_pipe_client *cli,
889 TALLOC_CTX *mem_ctx, int argc,
890 const char **argv)
892 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
893 WERROR werr = WERR_GENERAL_FAILURE;
894 const char *server_name = cli->desthost;
895 uint32_t trust_flags = NETR_TRUST_FLAG_IN_FOREST;
896 struct netr_DomainTrustList trusts;
897 struct dcerpc_binding_handle *b = cli->binding_handle;
899 if (argc < 1 || argc > 3) {
900 fprintf(stderr, "Usage: %s <server_name> <trust_flags>\n",
901 argv[0]);
902 return WERR_OK;
905 if (argc >= 2) {
906 server_name = argv[1];
909 if (argc >= 3) {
910 sscanf(argv[2], "%x", &trust_flags);
913 status = dcerpc_netr_DsrEnumerateDomainTrusts(b, mem_ctx,
914 server_name,
915 trust_flags,
916 &trusts,
917 &werr);
918 if (!NT_STATUS_IS_OK(status)) {
919 werr = ntstatus_to_werror(status);
920 goto done;
923 if (W_ERROR_IS_OK(werr)) {
924 int i;
926 printf("%d domains returned\n", trusts.count);
928 for (i=0; i<trusts.count; i++ ) {
929 printf("%s (%s)\n",
930 trusts.array[i].dns_name,
931 trusts.array[i].netbios_name);
934 done:
935 return werr;
938 static WERROR cmd_netlogon_deregisterdnsrecords(struct rpc_pipe_client *cli,
939 TALLOC_CTX *mem_ctx, int argc,
940 const char **argv)
942 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
943 WERROR werr = WERR_GENERAL_FAILURE;
944 const char *server_name = cli->desthost;
945 const char *domain = lp_workgroup();
946 const char *dns_host = NULL;
947 struct dcerpc_binding_handle *b = cli->binding_handle;
949 if (argc < 1 || argc > 4) {
950 fprintf(stderr, "Usage: %s <server_name> <domain_name> "
951 "<dns_host>\n", argv[0]);
952 return WERR_OK;
955 if (argc >= 2) {
956 server_name = argv[1];
959 if (argc >= 3) {
960 domain = argv[2];
963 if (argc >= 4) {
964 dns_host = argv[3];
967 status = dcerpc_netr_DsrDeregisterDNSHostRecords(b, mem_ctx,
968 server_name,
969 domain,
970 NULL,
971 NULL,
972 dns_host,
973 &werr);
974 if (!NT_STATUS_IS_OK(status)) {
975 werr = ntstatus_to_werror(status);
976 goto done;
979 if (W_ERROR_IS_OK(werr)) {
980 printf("success\n");
982 done:
983 return werr;
986 static WERROR cmd_netlogon_dsr_getforesttrustinfo(struct rpc_pipe_client *cli,
987 TALLOC_CTX *mem_ctx, int argc,
988 const char **argv)
990 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
991 WERROR werr = WERR_GENERAL_FAILURE;
992 const char *server_name = cli->desthost;
993 const char *trusted_domain_name = NULL;
994 struct lsa_ForestTrustInformation *info = NULL;
995 uint32_t flags = 0;
996 struct dcerpc_binding_handle *b = cli->binding_handle;
998 if (argc < 1 || argc > 4) {
999 fprintf(stderr, "Usage: %s <server_name> <trusted_domain_name> "
1000 "<flags>\n", argv[0]);
1001 return WERR_OK;
1004 if (argc >= 2) {
1005 server_name = argv[1];
1008 if (argc >= 3) {
1009 trusted_domain_name = argv[2];
1012 if (argc >= 4) {
1013 sscanf(argv[3], "%x", &flags);
1016 status = dcerpc_netr_DsRGetForestTrustInformation(b, mem_ctx,
1017 server_name,
1018 trusted_domain_name,
1019 flags,
1020 &info,
1021 &werr);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 werr = ntstatus_to_werror(status);
1024 goto done;
1027 if (W_ERROR_IS_OK(werr)) {
1028 printf("success\n");
1030 done:
1031 return werr;
1034 static NTSTATUS cmd_netlogon_enumtrusteddomains(struct rpc_pipe_client *cli,
1035 TALLOC_CTX *mem_ctx, int argc,
1036 const char **argv)
1038 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1039 NTSTATUS result;
1040 const char *server_name = cli->desthost;
1041 struct netr_Blob blob;
1042 struct dcerpc_binding_handle *b = cli->binding_handle;
1045 if (argc < 1 || argc > 3) {
1046 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
1047 return NT_STATUS_OK;
1050 if (argc >= 2) {
1051 server_name = argv[1];
1054 status = dcerpc_netr_NetrEnumerateTrustedDomains(b, mem_ctx,
1055 server_name,
1056 &blob,
1057 &result);
1058 if (!NT_STATUS_IS_OK(status)) {
1059 goto done;
1062 if (!NT_STATUS_IS_OK(result)) {
1063 status = result;
1064 goto done;
1067 printf("success\n");
1068 dump_data(1, blob.data, blob.length);
1069 done:
1070 return status;
1073 static WERROR cmd_netlogon_enumtrusteddomainsex(struct rpc_pipe_client *cli,
1074 TALLOC_CTX *mem_ctx, int argc,
1075 const char **argv)
1077 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1078 WERROR werr = WERR_GENERAL_FAILURE;
1079 const char *server_name = cli->desthost;
1080 struct netr_DomainTrustList list;
1081 struct dcerpc_binding_handle *b = cli->binding_handle;
1083 if (argc < 1 || argc > 3) {
1084 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
1085 return WERR_OK;
1088 if (argc >= 2) {
1089 server_name = argv[1];
1092 status = dcerpc_netr_NetrEnumerateTrustedDomainsEx(b, mem_ctx,
1093 server_name,
1094 &list,
1095 &werr);
1096 if (!NT_STATUS_IS_OK(status)) {
1097 werr = ntstatus_to_werror(status);
1098 goto done;
1101 if (W_ERROR_IS_OK(werr)) {
1102 printf("success\n");
1104 done:
1105 return werr;
1108 static WERROR cmd_netlogon_getdcsitecoverage(struct rpc_pipe_client *cli,
1109 TALLOC_CTX *mem_ctx, int argc,
1110 const char **argv)
1112 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1113 WERROR werr = WERR_GENERAL_FAILURE;
1114 const char *server_name = cli->desthost;
1115 struct DcSitesCtr *ctr = NULL;
1116 struct dcerpc_binding_handle *b = cli->binding_handle;
1118 if (argc < 1 || argc > 3) {
1119 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
1120 return WERR_OK;
1123 if (argc >= 2) {
1124 server_name = argv[1];
1127 status = dcerpc_netr_DsrGetDcSiteCoverageW(b, mem_ctx,
1128 server_name,
1129 &ctr,
1130 &werr);
1131 if (!NT_STATUS_IS_OK(status)) {
1132 werr = ntstatus_to_werror(status);
1133 goto done;
1136 if (W_ERROR_IS_OK(werr) && ctr->num_sites) {
1137 int i;
1138 printf("sites covered by this DC: %d\n", ctr->num_sites);
1139 for (i=0; i<ctr->num_sites; i++) {
1140 printf("%s\n", ctr->sites[i].string);
1143 done:
1144 return werr;
1147 static NTSTATUS cmd_netlogon_database_redo(struct rpc_pipe_client *cli,
1148 TALLOC_CTX *mem_ctx, int argc,
1149 const char **argv)
1151 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1152 NTSTATUS result;
1153 const char *server_name = cli->desthost;
1154 struct netr_Authenticator clnt_creds, srv_cred;
1155 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1156 struct netr_ChangeLogEntry e;
1157 uint32_t rid = 500;
1158 struct dcerpc_binding_handle *b = cli->binding_handle;
1159 struct netlogon_creds_CredentialState *creds = NULL;
1161 if (argc > 2) {
1162 fprintf(stderr, "Usage: %s <user rid>\n", argv[0]);
1163 return NT_STATUS_OK;
1166 if (argc == 2) {
1167 sscanf(argv[1], "%d", &rid);
1170 if (rpcclient_netlogon_creds == NULL) {
1171 return NT_STATUS_UNSUCCESSFUL;
1174 status = netlogon_creds_cli_lock(rpcclient_netlogon_creds,
1175 mem_ctx, &creds);
1176 if (!NT_STATUS_IS_OK(status)) {
1177 return status;
1180 netlogon_creds_client_authenticator(creds, &clnt_creds);
1182 ZERO_STRUCT(e);
1184 e.object_rid = rid;
1185 e.db_index = SAM_DATABASE_DOMAIN;
1186 e.delta_type = NETR_DELTA_USER;
1188 status = dcerpc_netr_DatabaseRedo(b, mem_ctx,
1189 server_name,
1190 lp_netbios_name(),
1191 &clnt_creds,
1192 &srv_cred,
1194 0, /* is calculated automatically */
1195 &delta_enum_array,
1196 &result);
1197 if (!NT_STATUS_IS_OK(status)) {
1198 TALLOC_FREE(creds);
1199 return status;
1202 if (!netlogon_creds_client_check(creds, &srv_cred.cred)) {
1203 DEBUG(0,("credentials chain check failed\n"));
1204 TALLOC_FREE(creds);
1205 return NT_STATUS_ACCESS_DENIED;
1207 TALLOC_FREE(creds);
1209 return result;
1212 static NTSTATUS cmd_netlogon_capabilities(struct rpc_pipe_client *cli,
1213 TALLOC_CTX *mem_ctx, int argc,
1214 const char **argv)
1216 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1217 NTSTATUS result;
1218 struct netr_Authenticator credential;
1219 struct netr_Authenticator return_authenticator;
1220 union netr_Capabilities capabilities;
1221 uint32_t level = 1;
1222 struct dcerpc_binding_handle *b = cli->binding_handle;
1223 struct netlogon_creds_CredentialState *creds = NULL;
1225 if (argc > 2) {
1226 fprintf(stderr, "Usage: %s <level>\n", argv[0]);
1227 return NT_STATUS_OK;
1230 if (argc == 2) {
1231 level = atoi(argv[1]);
1234 ZERO_STRUCT(return_authenticator);
1236 if (rpcclient_netlogon_creds == NULL) {
1237 return NT_STATUS_UNSUCCESSFUL;
1240 status = netlogon_creds_cli_lock(rpcclient_netlogon_creds,
1241 mem_ctx, &creds);
1242 if (!NT_STATUS_IS_OK(status)) {
1243 return status;
1246 netlogon_creds_client_authenticator(creds, &credential);
1248 status = dcerpc_netr_LogonGetCapabilities(b, mem_ctx,
1249 cli->desthost,
1250 lp_netbios_name(),
1251 &credential,
1252 &return_authenticator,
1253 level,
1254 &capabilities,
1255 &result);
1256 if (!NT_STATUS_IS_OK(status)) {
1257 TALLOC_FREE(creds);
1258 return status;
1261 if (!netlogon_creds_client_check(creds,
1262 &return_authenticator.cred)) {
1263 DEBUG(0,("credentials chain check failed\n"));
1264 TALLOC_FREE(creds);
1265 return NT_STATUS_ACCESS_DENIED;
1267 TALLOC_FREE(creds);
1269 printf("capabilities: 0x%08x\n", capabilities.server_capabilities);
1271 return result;
1274 /* List of commands exported by this module */
1276 struct cmd_set netlogon_commands[] = {
1278 { "NETLOGON" },
1280 { "logonctrl2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl2, &ndr_table_netlogon, NULL, "Logon Control 2", "" },
1281 { "getanydcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getanydcname, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1282 { "getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcname, &ndr_table_netlogon, NULL, "Get trusted PDC name", "" },
1283 { "dsr_getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcname, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1284 { "dsr_getdcnameex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1285 { "dsr_getdcnameex2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex2, &ndr_table_netlogon, NULL, "Get trusted DC name", "" },
1286 { "dsr_getsitename", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getsitename, &ndr_table_netlogon, NULL, "Get sitename", "" },
1287 { "dsr_getforesttrustinfo", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getforesttrustinfo, &ndr_table_netlogon, NULL, "Get Forest Trust Info", "" },
1288 { "logonctrl", RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl, &ndr_table_netlogon, NULL, "Logon Control", "" },
1289 { "samsync", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_sync, NULL, &ndr_table_netlogon, NULL, "Sam Synchronisation", "" },
1290 { "samdeltas", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_deltas, NULL, &ndr_table_netlogon, NULL, "Query Sam Deltas", "" },
1291 { "samlogon", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_logon, NULL, &ndr_table_netlogon, NULL, "Sam Logon", "" },
1292 { "change_trust_pw", RPC_RTYPE_NTSTATUS, cmd_netlogon_change_trust_pw, NULL, &ndr_table_netlogon, NULL, "Change Trust Account Password", "" },
1293 { "gettrustrid", RPC_RTYPE_WERROR, NULL, cmd_netlogon_gettrustrid, &ndr_table_netlogon, NULL, "Get trust rid", "" },
1294 { "dsr_enumtrustdom", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon, NULL, "Enumerate trusted domains", "" },
1295 { "dsenumdomtrusts", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon, NULL, "Enumerate all trusted domains in an AD forest", "" },
1296 { "deregisterdnsrecords", RPC_RTYPE_WERROR, NULL, cmd_netlogon_deregisterdnsrecords, &ndr_table_netlogon, NULL, "Deregister DNS records", "" },
1297 { "netrenumtrusteddomains", RPC_RTYPE_NTSTATUS, cmd_netlogon_enumtrusteddomains, NULL, &ndr_table_netlogon, NULL, "Enumerate trusted domains", "" },
1298 { "netrenumtrusteddomainsex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_enumtrusteddomainsex, &ndr_table_netlogon, NULL, "Enumerate trusted domains", "" },
1299 { "getdcsitecoverage", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcsitecoverage, &ndr_table_netlogon, NULL, "Get the Site-Coverage from a DC", "" },
1300 { "database_redo", RPC_RTYPE_NTSTATUS, cmd_netlogon_database_redo, NULL, &ndr_table_netlogon, NULL, "Replicate single object from a DC", "" },
1301 { "capabilities", RPC_RTYPE_NTSTATUS, cmd_netlogon_capabilities, NULL, &ndr_table_netlogon, NULL, "Return Capabilities", "" },
1303 { NULL }