faa2bfff3efaf35cec28193d4a159659d06cc2d8
[nivanova/samba-autobuild/.git] / source3 / rpcclient / cmd_netlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4
5    Copyright (C) Tim Potter 2000
6    Copyright (C) Guenther Deschner 2008
7
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "rpcclient.h"
24 #include "../libcli/auth/libcli_auth.h"
25 #include "../librpc/gen_ndr/cli_netlogon.h"
26 #include "rpc_client/cli_netlogon.h"
27
28 static WERROR cmd_netlogon_logon_ctrl2(struct rpc_pipe_client *cli,
29                                        TALLOC_CTX *mem_ctx, int argc,
30                                        const char **argv)
31 {
32         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
33         WERROR werr;
34         const char *logon_server = cli->desthost;
35         enum netr_LogonControlCode function_code = NETLOGON_CONTROL_REDISCOVER;
36         uint32_t level = 1;
37         union netr_CONTROL_DATA_INFORMATION data;
38         union netr_CONTROL_QUERY_INFORMATION query;
39         const char *domain = lp_workgroup();
40
41         if (argc > 5) {
42                 fprintf(stderr, "Usage: %s <logon_server> <function_code> "
43                         "<level> <domain>\n", argv[0]);
44                 return WERR_OK;
45         }
46
47         if (argc >= 2) {
48                 logon_server = argv[1];
49         }
50
51         if (argc >= 3) {
52                 function_code = atoi(argv[2]);
53         }
54
55         if (argc >= 4) {
56                 level = atoi(argv[3]);
57         }
58
59         if (argc >= 5) {
60                 domain = argv[4];
61         }
62
63         switch (function_code) {
64                 case NETLOGON_CONTROL_REDISCOVER:
65                 case NETLOGON_CONTROL_TC_QUERY:
66                         data.domain = domain;
67                         break;
68                 default:
69                         break;
70         }
71
72         status = rpccli_netr_LogonControl2(cli, mem_ctx,
73                                           logon_server,
74                                           function_code,
75                                           level,
76                                           &data,
77                                           &query,
78                                           &werr);
79         if (!NT_STATUS_IS_OK(status)) {
80                 return ntstatus_to_werror(status);
81         }
82
83         if (!W_ERROR_IS_OK(werr)) {
84                 return werr;
85         }
86
87         /* Display results */
88
89         return werr;
90 }
91
92 static WERROR cmd_netlogon_getanydcname(struct rpc_pipe_client *cli, 
93                                         TALLOC_CTX *mem_ctx, int argc, 
94                                         const char **argv)
95 {
96         const char *dcname = NULL;
97         WERROR werr;
98         NTSTATUS status;
99         int old_timeout;
100
101         if (argc != 2) {
102                 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
103                 return WERR_OK;
104         }
105
106         /* Make sure to wait for our DC's reply */
107         old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
108         rpccli_set_timeout(cli, MAX(old_timeout, 30000)); /* At least 30 sec */
109
110         status = rpccli_netr_GetAnyDCName(cli, mem_ctx,
111                                           cli->desthost,
112                                           argv[1],
113                                           &dcname,
114                                           &werr);
115         rpccli_set_timeout(cli, old_timeout);
116
117         if (!NT_STATUS_IS_OK(status)) {
118                 return ntstatus_to_werror(status);
119         }
120
121         if (!W_ERROR_IS_OK(werr)) {
122                 return werr;
123         }
124
125         /* Display results */
126
127         printf("%s\n", dcname);
128
129         return werr;
130 }
131
132 static WERROR cmd_netlogon_getdcname(struct rpc_pipe_client *cli,
133                                      TALLOC_CTX *mem_ctx, int argc,
134                                      const char **argv)
135 {
136         const char *dcname = NULL;
137         NTSTATUS status;
138         WERROR werr;
139         int old_timeout;
140
141         if (argc != 2) {
142                 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
143                 return WERR_OK;
144         }
145
146         /* Make sure to wait for our DC's reply */
147         old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
148         rpccli_set_timeout(cli, MAX(30000, old_timeout)); /* At least 30 sec */
149
150         status = rpccli_netr_GetDcName(cli, mem_ctx,
151                                        cli->desthost,
152                                        argv[1],
153                                        &dcname,
154                                        &werr);
155         rpccli_set_timeout(cli, old_timeout);
156
157         if (!NT_STATUS_IS_OK(status)) {
158                 return ntstatus_to_werror(status);
159         }
160
161         if (!W_ERROR_IS_OK(werr)) {
162                 return werr;
163         }
164
165         /* Display results */
166
167         printf("%s\n", dcname);
168
169         return werr;
170 }
171
172 static WERROR cmd_netlogon_dsr_getdcname(struct rpc_pipe_client *cli,
173                                          TALLOC_CTX *mem_ctx, int argc,
174                                          const char **argv)
175 {
176         NTSTATUS result;
177         WERROR werr = WERR_OK;
178         uint32 flags = DS_RETURN_DNS_NAME;
179         const char *server_name = cli->desthost;
180         const char *domain_name;
181         struct GUID domain_guid = GUID_zero();
182         struct GUID site_guid = GUID_zero();
183         struct netr_DsRGetDCNameInfo *info = NULL;
184
185         if (argc < 2) {
186                 fprintf(stderr, "Usage: %s [domain_name] [domain_guid] "
187                                 "[site_guid] [flags]\n", argv[0]);
188                 return WERR_OK;
189         }
190
191         if (argc >= 2)
192                 domain_name = argv[1];
193
194         if (argc >= 3) {
195                 if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
196                         return WERR_NOMEM;
197                 }
198         }
199
200         if (argc >= 4) {
201                 if (!NT_STATUS_IS_OK(GUID_from_string(argv[3], &site_guid))) {
202                         return WERR_NOMEM;
203                 }
204         }
205
206         if (argc >= 5)
207                 sscanf(argv[4], "%x", &flags);
208
209         debug_dsdcinfo_flags(1,flags);
210
211         result = rpccli_netr_DsRGetDCName(cli, mem_ctx,
212                                           server_name,
213                                           domain_name,
214                                           &domain_guid,
215                                           &site_guid,
216                                           flags,
217                                           &info,
218                                           &werr);
219
220         if (W_ERROR_IS_OK(werr)) {
221                 d_printf("DsGetDcName gave: %s\n",
222                 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
223                 return WERR_OK;
224         }
225
226         printf("rpccli_netlogon_dsr_getdcname returned %s\n",
227                win_errstr(werr));
228
229         return werr;
230 }
231
232 static WERROR cmd_netlogon_dsr_getdcnameex(struct rpc_pipe_client *cli,
233                                            TALLOC_CTX *mem_ctx, int argc,
234                                            const char **argv)
235 {
236         WERROR result;
237         NTSTATUS status;
238         uint32_t flags = DS_RETURN_DNS_NAME;
239         const char *server_name = cli->desthost;
240         const char *domain_name;
241         const char *site_name = NULL;
242         struct GUID domain_guid = GUID_zero();
243         struct netr_DsRGetDCNameInfo *info = NULL;
244
245         if (argc < 2) {
246                 fprintf(stderr, "Usage: %s [domain_name] [domain_guid] "
247                                 "[site_name] [flags]\n", argv[0]);
248                 return WERR_OK;
249         }
250
251         domain_name = argv[1];
252
253         if (argc >= 3) {
254                 if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
255                         return WERR_NOMEM;
256                 }
257         }
258
259         if (argc >= 4) {
260                 site_name = argv[3];
261         }
262
263         if (argc >= 5) {
264                 sscanf(argv[4], "%x", &flags);
265         }
266
267         debug_dsdcinfo_flags(1,flags);
268
269         status = rpccli_netr_DsRGetDCNameEx(cli, mem_ctx,
270                                             server_name,
271                                             domain_name,
272                                             &domain_guid,
273                                             site_name,
274                                             flags,
275                                             &info,
276                                             &result);
277         if (!NT_STATUS_IS_OK(status)) {
278                 return ntstatus_to_werror(status);
279         }
280
281         if (!W_ERROR_IS_OK(result)) {
282                 return result;
283         }
284
285         d_printf("DsRGetDCNameEx gave %s\n",
286                 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
287
288         return result;
289 }
290
291 static WERROR cmd_netlogon_dsr_getdcnameex2(struct rpc_pipe_client *cli,
292                                             TALLOC_CTX *mem_ctx, int argc,
293                                             const char **argv)
294 {
295         WERROR result;
296         NTSTATUS status;
297         uint32_t flags = DS_RETURN_DNS_NAME;
298         const char *server_name = cli->desthost;
299         const char *domain_name = NULL;
300         const char *client_account = NULL;
301         uint32_t mask = 0;
302         const char *site_name = NULL;
303         struct GUID domain_guid = GUID_zero();
304         struct netr_DsRGetDCNameInfo *info = NULL;
305
306         if (argc < 2) {
307                 fprintf(stderr, "Usage: %s [client_account] [acb_mask] "
308                                 "[domain_name] [domain_guid] [site_name] "
309                                 "[flags]\n", argv[0]);
310                 return WERR_OK;
311         }
312
313         if (argc >= 2) {
314                 client_account = argv[1];
315         }
316
317         if (argc >= 3) {
318                 mask = atoi(argv[2]);
319         }
320
321         if (argc >= 4) {
322                 domain_name = argv[3];
323         }
324
325         if (argc >= 5) {
326                 if (!NT_STATUS_IS_OK(GUID_from_string(argv[4], &domain_guid))) {
327                         return WERR_NOMEM;
328                 }
329         }
330
331         if (argc >= 6) {
332                 site_name = argv[5];
333         }
334
335         if (argc >= 7) {
336                 sscanf(argv[6], "%x", &flags);
337         }
338
339         debug_dsdcinfo_flags(1,flags);
340
341         status = rpccli_netr_DsRGetDCNameEx2(cli, mem_ctx,
342                                              server_name,
343                                              client_account,
344                                              mask,
345                                              domain_name,
346                                              &domain_guid,
347                                              site_name,
348                                              flags,
349                                              &info,
350                                              &result);
351         if (!NT_STATUS_IS_OK(status)) {
352                 return ntstatus_to_werror(status);
353         }
354
355         if (!W_ERROR_IS_OK(result)) {
356                 return result;
357         }
358
359         d_printf("DsRGetDCNameEx2 gave %s\n",
360                 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
361
362         return result;
363 }
364
365
366 static WERROR cmd_netlogon_dsr_getsitename(struct rpc_pipe_client *cli,
367                                            TALLOC_CTX *mem_ctx, int argc,
368                                            const char **argv)
369 {
370         WERROR werr;
371         NTSTATUS status;
372         const char *sitename = NULL;
373
374         if (argc != 2) {
375                 fprintf(stderr, "Usage: %s computername\n", argv[0]);
376                 return WERR_OK;
377         }
378
379         status = rpccli_netr_DsRGetSiteName(cli, mem_ctx,
380                                             argv[1],
381                                             &sitename,
382                                             &werr);
383         if (!NT_STATUS_IS_OK(status)) {
384                 return ntstatus_to_werror(status);
385         }
386
387         if (!W_ERROR_IS_OK(werr)) {
388                 printf("rpccli_netlogon_dsr_gesitename returned %s\n",
389                        nt_errstr(werror_to_ntstatus(werr)));
390                 return werr;
391         }
392
393         printf("Computer %s is on Site: %s\n", argv[1], sitename);
394
395         return WERR_OK;
396 }
397
398 static WERROR cmd_netlogon_logon_ctrl(struct rpc_pipe_client *cli,
399                                       TALLOC_CTX *mem_ctx, int argc,
400                                       const char **argv)
401 {
402         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
403         WERROR werr;
404         const char *logon_server = cli->desthost;
405         enum netr_LogonControlCode function_code = 1;
406         uint32_t level = 1;
407         union netr_CONTROL_QUERY_INFORMATION info;
408
409         if (argc > 4) {
410                 fprintf(stderr, "Usage: %s <logon_server> <function_code> "
411                         "<level>\n", argv[0]);
412                 return WERR_OK;
413         }
414
415         if (argc >= 2) {
416                 logon_server = argv[1];
417         }
418
419         if (argc >= 3) {
420                 function_code = atoi(argv[2]);
421         }
422
423         if (argc >= 4) {
424                 level = atoi(argv[3]);
425         }
426
427         status = rpccli_netr_LogonControl(cli, mem_ctx,
428                                           logon_server,
429                                           function_code,
430                                           level,
431                                           &info,
432                                           &werr);
433         if (!NT_STATUS_IS_OK(status)) {
434                 return ntstatus_to_werror(status);
435         }
436
437         if (!W_ERROR_IS_OK(werr)) {
438                 return werr;
439         }
440
441         /* Display results */
442
443         return werr;
444 }
445
446 /* Display sam synchronisation information */
447
448 static void display_sam_sync(struct netr_DELTA_ENUM_ARRAY *r)
449 {
450         uint32_t i, j;
451
452         for (i=0; i < r->num_deltas; i++) {
453
454                 union netr_DELTA_UNION u = r->delta_enum[i].delta_union;
455                 union netr_DELTA_ID_UNION id = r->delta_enum[i].delta_id_union;
456
457                 switch (r->delta_enum[i].delta_type) {
458                 case NETR_DELTA_DOMAIN:
459                         printf("Domain: %s\n",
460                                 u.domain->domain_name.string);
461                         break;
462                 case NETR_DELTA_GROUP:
463                         printf("Group: %s\n",
464                                 u.group->group_name.string);
465                         break;
466                 case NETR_DELTA_DELETE_GROUP:
467                         printf("Delete Group: %d\n",
468                                 u.delete_account.unknown);
469                         break;
470                 case NETR_DELTA_RENAME_GROUP:
471                         printf("Rename Group: %s -> %s\n",
472                                 u.rename_group->OldName.string,
473                                 u.rename_group->NewName.string);
474                         break;
475                 case NETR_DELTA_USER:
476                         printf("Account: %s\n",
477                                 u.user->account_name.string);
478                         break;
479                 case NETR_DELTA_DELETE_USER:
480                         printf("Delete User: %d\n",
481                                 id.rid);
482                         break;
483                 case NETR_DELTA_RENAME_USER:
484                         printf("Rename user: %s -> %s\n",
485                                 u.rename_user->OldName.string,
486                                 u.rename_user->NewName.string);
487                         break;
488                 case NETR_DELTA_GROUP_MEMBER:
489                         for (j=0; j < u.group_member->num_rids; j++) {
490                                 printf("rid 0x%x, attrib 0x%08x\n",
491                                         u.group_member->rids[j],
492                                         u.group_member->attribs[j]);
493                         }
494                         break;
495                 case NETR_DELTA_ALIAS:
496                         printf("Alias: %s\n",
497                                 u.alias->alias_name.string);
498                         break;
499                 case NETR_DELTA_DELETE_ALIAS:
500                         printf("Delete Alias: %d\n",
501                                 r->delta_enum[i].delta_id_union.rid);
502                         break;
503                 case NETR_DELTA_RENAME_ALIAS:
504                         printf("Rename alias: %s -> %s\n",
505                                 u.rename_alias->OldName.string,
506                                 u.rename_alias->NewName.string);
507                         break;
508                 case NETR_DELTA_ALIAS_MEMBER:
509                         for (j=0; j < u.alias_member->sids.num_sids; j++) {
510                                 fstring sid_str;
511                                 sid_to_fstring(sid_str,
512                                         u.alias_member->sids.sids[j].sid);
513                                 printf("%s\n", sid_str);
514                         }
515                         break;
516                 case NETR_DELTA_POLICY:
517                         printf("Policy\n");
518                         break;
519                 case NETR_DELTA_TRUSTED_DOMAIN:
520                         printf("Trusted Domain: %s\n",
521                                 u.trusted_domain->domain_name.string);
522                         break;
523                 case NETR_DELTA_DELETE_TRUST:
524                         printf("Delete Trust: %d\n",
525                                 u.delete_trust.unknown);
526                         break;
527                 case NETR_DELTA_ACCOUNT:
528                         printf("Account\n");
529                         break;
530                 case NETR_DELTA_DELETE_ACCOUNT:
531                         printf("Delete Account: %d\n",
532                                 u.delete_account.unknown);
533                         break;
534                 case NETR_DELTA_SECRET:
535                         printf("Secret\n");
536                         break;
537                 case NETR_DELTA_DELETE_SECRET:
538                         printf("Delete Secret: %d\n",
539                                 u.delete_secret.unknown);
540                         break;
541                 case NETR_DELTA_DELETE_GROUP2:
542                         printf("Delete Group2: %s\n",
543                                 u.delete_group->account_name);
544                         break;
545                 case NETR_DELTA_DELETE_USER2:
546                         printf("Delete User2: %s\n",
547                                 u.delete_user->account_name);
548                         break;
549                 case NETR_DELTA_MODIFY_COUNT:
550                         printf("sam sequence update: 0x%016llx\n",
551                                 (unsigned long long) *u.modified_count);
552                         break;
553                 default:
554                         printf("unknown delta type 0x%02x\n",
555                                 r->delta_enum[i].delta_type);
556                         break;
557                 }
558         }
559 }
560
561 /* Perform sam synchronisation */
562
563 static NTSTATUS cmd_netlogon_sam_sync(struct rpc_pipe_client *cli,
564                                       TALLOC_CTX *mem_ctx, int argc,
565                                       const char **argv)
566 {
567         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
568         const char *logon_server = cli->desthost;
569         const char *computername = global_myname();
570         struct netr_Authenticator credential;
571         struct netr_Authenticator return_authenticator;
572         enum netr_SamDatabaseID database_id = SAM_DATABASE_DOMAIN;
573         uint16_t restart_state = 0;
574         uint32_t sync_context = 0;
575
576         if (argc > 2) {
577                 fprintf(stderr, "Usage: %s [database_id]\n", argv[0]);
578                 return NT_STATUS_OK;
579         }
580
581         if (argc == 2) {
582                 database_id = atoi(argv[1]);
583         }
584
585         /* Synchronise sam database */
586
587         do {
588                 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
589
590                 netlogon_creds_client_authenticator(cli->dc, &credential);
591
592                 result = rpccli_netr_DatabaseSync2(cli, mem_ctx,
593                                                    logon_server,
594                                                    computername,
595                                                    &credential,
596                                                    &return_authenticator,
597                                                    database_id,
598                                                    restart_state,
599                                                    &sync_context,
600                                                    &delta_enum_array,
601                                                    0xffff);
602
603                 /* Check returned credentials. */
604                 if (!netlogon_creds_client_check(cli->dc,
605                                                  &return_authenticator.cred)) {
606                         DEBUG(0,("credentials chain check failed\n"));
607                         return NT_STATUS_ACCESS_DENIED;
608                 }
609
610                 if (NT_STATUS_IS_ERR(result)) {
611                         break;
612                 }
613
614                 /* Display results */
615
616                 display_sam_sync(delta_enum_array);
617
618                 TALLOC_FREE(delta_enum_array);
619
620         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
621
622         return result;
623 }
624
625 /* Perform sam delta synchronisation */
626
627 static NTSTATUS cmd_netlogon_sam_deltas(struct rpc_pipe_client *cli,
628                                         TALLOC_CTX *mem_ctx, int argc,
629                                         const char **argv)
630 {
631         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
632         uint32_t tmp;
633         const char *logon_server = cli->desthost;
634         const char *computername = global_myname();
635         struct netr_Authenticator credential;
636         struct netr_Authenticator return_authenticator;
637         enum netr_SamDatabaseID database_id = SAM_DATABASE_DOMAIN;
638         uint64_t sequence_num;
639
640         if (argc != 3) {
641                 fprintf(stderr, "Usage: %s database_id seqnum\n", argv[0]);
642                 return NT_STATUS_OK;
643         }
644
645         database_id = atoi(argv[1]);
646         tmp = atoi(argv[2]);
647
648         sequence_num = tmp & 0xffff;
649
650         do {
651                 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
652
653                 netlogon_creds_client_authenticator(cli->dc, &credential);
654
655                 result = rpccli_netr_DatabaseDeltas(cli, mem_ctx,
656                                                     logon_server,
657                                                     computername,
658                                                     &credential,
659                                                     &return_authenticator,
660                                                     database_id,
661                                                     &sequence_num,
662                                                     &delta_enum_array,
663                                                     0xffff);
664
665                 /* Check returned credentials. */
666                 if (!netlogon_creds_client_check(cli->dc,
667                                                  &return_authenticator.cred)) {
668                         DEBUG(0,("credentials chain check failed\n"));
669                         return NT_STATUS_ACCESS_DENIED;
670                 }
671
672                 if (NT_STATUS_IS_ERR(result)) {
673                         break;
674                 }
675
676                 /* Display results */
677
678                 display_sam_sync(delta_enum_array);
679
680                 TALLOC_FREE(delta_enum_array);
681
682         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
683
684         return result;
685 }
686
687 /* Log on a domain user */
688
689 static NTSTATUS cmd_netlogon_sam_logon(struct rpc_pipe_client *cli, 
690                                        TALLOC_CTX *mem_ctx, int argc,
691                                        const char **argv)
692 {
693         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
694         int logon_type = NetlogonNetworkInformation;
695         const char *username, *password;
696         int auth_level = 2;
697         uint32 logon_param = 0;
698         const char *workstation = NULL;
699
700         /* Check arguments */
701
702         if (argc < 3 || argc > 7) {
703                 fprintf(stderr, "Usage: samlogon <username> <password> [workstation]"
704                         "[logon_type (1 or 2)] [auth level (2 or 3)] [logon_parameter]\n");
705                 return NT_STATUS_OK;
706         }
707
708         username = argv[1];
709         password = argv[2];
710
711         if (argc >= 4) 
712                 workstation = argv[3];
713
714         if (argc >= 5)
715                 sscanf(argv[4], "%i", &logon_type);
716
717         if (argc >= 6)
718                 sscanf(argv[5], "%i", &auth_level);
719
720         if (argc == 7)
721                 sscanf(argv[6], "%x", &logon_param);
722
723         /* Perform the sam logon */
724
725         result = rpccli_netlogon_sam_logon(cli, mem_ctx, logon_param, lp_workgroup(), username, password, workstation, logon_type);
726
727         if (!NT_STATUS_IS_OK(result))
728                 goto done;
729
730  done:
731         return result;
732 }
733
734 /* Change the trust account password */
735
736 static NTSTATUS cmd_netlogon_change_trust_pw(struct rpc_pipe_client *cli, 
737                                              TALLOC_CTX *mem_ctx, int argc,
738                                              const char **argv)
739 {
740         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
741
742         /* Check arguments */
743
744         if (argc > 1) {
745                 fprintf(stderr, "Usage: change_trust_pw");
746                 return NT_STATUS_OK;
747         }
748
749         /* Perform the sam logon */
750
751         result = trust_pw_find_change_and_store_it(cli, mem_ctx,
752                                                    lp_workgroup());
753
754         if (!NT_STATUS_IS_OK(result))
755                 goto done;
756
757  done:
758         return result;
759 }
760
761 static WERROR cmd_netlogon_gettrustrid(struct rpc_pipe_client *cli,
762                                        TALLOC_CTX *mem_ctx, int argc,
763                                        const char **argv)
764 {
765         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
766         WERROR werr = WERR_GENERAL_FAILURE;
767         const char *server_name = cli->desthost;
768         const char *domain_name = lp_workgroup();
769         uint32_t rid = 0;
770
771         if (argc < 1 || argc > 3) {
772                 fprintf(stderr, "Usage: %s <server_name> <domain_name>\n",
773                         argv[0]);
774                 return WERR_OK;
775         }
776
777         if (argc >= 2) {
778                 server_name = argv[1];
779         }
780
781         if (argc >= 3) {
782                 domain_name = argv[2];
783         }
784
785         status = rpccli_netr_LogonGetTrustRid(cli, mem_ctx,
786                                               server_name,
787                                               domain_name,
788                                               &rid,
789                                               &werr);
790         if (!NT_STATUS_IS_OK(status)) {
791                 goto done;
792         }
793
794         if (W_ERROR_IS_OK(werr)) {
795                 printf("Rid: %d\n", rid);
796         }
797  done:
798         return werr;
799 }
800
801 static WERROR cmd_netlogon_dsr_enumtrustdom(struct rpc_pipe_client *cli,
802                                             TALLOC_CTX *mem_ctx, int argc,
803                                             const char **argv)
804 {
805         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
806         WERROR werr = WERR_GENERAL_FAILURE;
807         const char *server_name = cli->desthost;
808         uint32_t trust_flags = NETR_TRUST_FLAG_IN_FOREST;
809         struct netr_DomainTrustList trusts;
810
811         if (argc < 1 || argc > 3) {
812                 fprintf(stderr, "Usage: %s <server_name> <trust_flags>\n",
813                         argv[0]);
814                 return WERR_OK;
815         }
816
817         if (argc >= 2) {
818                 server_name = argv[1];
819         }
820
821         if (argc >= 3) {
822                 sscanf(argv[2], "%x", &trust_flags);
823         }
824
825         status = rpccli_netr_DsrEnumerateDomainTrusts(cli, mem_ctx,
826                                                       server_name,
827                                                       trust_flags,
828                                                       &trusts,
829                                                       &werr);
830         if (!NT_STATUS_IS_OK(status)) {
831                 goto done;
832         }
833
834         if (W_ERROR_IS_OK(werr)) {
835                 int i;
836
837                 printf("%d domains returned\n", trusts.count);
838
839                 for (i=0; i<trusts.count; i++ ) {
840                         printf("%s (%s)\n",
841                                 trusts.array[i].dns_name,
842                                 trusts.array[i].netbios_name);
843                 }
844         }
845  done:
846         return werr;
847 }
848
849 static WERROR cmd_netlogon_deregisterdnsrecords(struct rpc_pipe_client *cli,
850                                                 TALLOC_CTX *mem_ctx, int argc,
851                                                 const char **argv)
852 {
853         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
854         WERROR werr = WERR_GENERAL_FAILURE;
855         const char *server_name = cli->desthost;
856         const char *domain = lp_workgroup();
857         const char *dns_host = NULL;
858
859         if (argc < 1 || argc > 4) {
860                 fprintf(stderr, "Usage: %s <server_name> <domain_name> "
861                         "<dns_host>\n", argv[0]);
862                 return WERR_OK;
863         }
864
865         if (argc >= 2) {
866                 server_name = argv[1];
867         }
868
869         if (argc >= 3) {
870                 domain = argv[2];
871         }
872
873         if (argc >= 4) {
874                 dns_host = argv[3];
875         }
876
877         status = rpccli_netr_DsrDeregisterDNSHostRecords(cli, mem_ctx,
878                                                          server_name,
879                                                          domain,
880                                                          NULL,
881                                                          NULL,
882                                                          dns_host,
883                                                          &werr);
884         if (!NT_STATUS_IS_OK(status)) {
885                 goto done;
886         }
887
888         if (W_ERROR_IS_OK(werr)) {
889                 printf("success\n");
890         }
891  done:
892         return werr;
893 }
894
895 static WERROR cmd_netlogon_dsr_getforesttrustinfo(struct rpc_pipe_client *cli,
896                                                   TALLOC_CTX *mem_ctx, int argc,
897                                                   const char **argv)
898 {
899         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
900         WERROR werr = WERR_GENERAL_FAILURE;
901         const char *server_name = cli->desthost;
902         const char *trusted_domain_name = NULL;
903         struct lsa_ForestTrustInformation *info = NULL;
904         uint32_t flags = 0;
905
906         if (argc < 1 || argc > 4) {
907                 fprintf(stderr, "Usage: %s <server_name> <trusted_domain_name> "
908                         "<flags>\n", argv[0]);
909                 return WERR_OK;
910         }
911
912         if (argc >= 2) {
913                 server_name = argv[1];
914         }
915
916         if (argc >= 3) {
917                 trusted_domain_name = argv[2];
918         }
919
920         if (argc >= 4) {
921                 sscanf(argv[3], "%x", &flags);
922         }
923
924         status = rpccli_netr_DsRGetForestTrustInformation(cli, mem_ctx,
925                                                          server_name,
926                                                          trusted_domain_name,
927                                                          flags,
928                                                          &info,
929                                                          &werr);
930         if (!NT_STATUS_IS_OK(status)) {
931                 goto done;
932         }
933
934         if (W_ERROR_IS_OK(werr)) {
935                 printf("success\n");
936         }
937  done:
938         return werr;
939 }
940
941 static NTSTATUS cmd_netlogon_enumtrusteddomains(struct rpc_pipe_client *cli,
942                                                 TALLOC_CTX *mem_ctx, int argc,
943                                                 const char **argv)
944 {
945         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
946         const char *server_name = cli->desthost;
947         struct netr_Blob blob;
948
949
950         if (argc < 1 || argc > 3) {
951                 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
952                 return NT_STATUS_OK;
953         }
954
955         if (argc >= 2) {
956                 server_name = argv[1];
957         }
958
959         status = rpccli_netr_NetrEnumerateTrustedDomains(cli, mem_ctx,
960                                                          server_name,
961                                                          &blob);
962         if (!NT_STATUS_IS_OK(status)) {
963                 goto done;
964         }
965
966         printf("success\n");
967         dump_data(1, blob.data, blob.length);
968  done:
969         return status;
970 }
971
972 static WERROR cmd_netlogon_enumtrusteddomainsex(struct rpc_pipe_client *cli,
973                                                 TALLOC_CTX *mem_ctx, int argc,
974                                                 const char **argv)
975 {
976         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
977         WERROR werr = WERR_GENERAL_FAILURE;
978         const char *server_name = cli->desthost;
979         struct netr_DomainTrustList list;
980
981         if (argc < 1 || argc > 3) {
982                 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
983                 return WERR_OK;
984         }
985
986         if (argc >= 2) {
987                 server_name = argv[1];
988         }
989
990         status = rpccli_netr_NetrEnumerateTrustedDomainsEx(cli, mem_ctx,
991                                                            server_name,
992                                                            &list,
993                                                            &werr);
994         if (!NT_STATUS_IS_OK(status)) {
995                 goto done;
996         }
997
998         if (W_ERROR_IS_OK(werr)) {
999                 printf("success\n");
1000         }
1001  done:
1002         return werr;
1003 }
1004
1005 static WERROR cmd_netlogon_getdcsitecoverage(struct rpc_pipe_client *cli,
1006                                              TALLOC_CTX *mem_ctx, int argc,
1007                                              const char **argv)
1008 {
1009         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1010         WERROR werr = WERR_GENERAL_FAILURE;
1011         const char *server_name = cli->desthost;
1012         struct DcSitesCtr *ctr = NULL;
1013
1014         if (argc < 1 || argc > 3) {
1015                 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
1016                 return WERR_OK;
1017         }
1018
1019         if (argc >= 2) {
1020                 server_name = argv[1];
1021         }
1022
1023         status = rpccli_netr_DsrGetDcSiteCoverageW(cli, mem_ctx,
1024                                                    server_name,
1025                                                    &ctr,
1026                                                    &werr);
1027         if (!NT_STATUS_IS_OK(status)) {
1028                 goto done;
1029         }
1030
1031         if (W_ERROR_IS_OK(werr) && ctr->num_sites) {
1032                 int i;
1033                 printf("sites covered by this DC: %d\n", ctr->num_sites);
1034                 for (i=0; i<ctr->num_sites; i++) {
1035                         printf("%s\n", ctr->sites[i].string);
1036                 }
1037         }
1038  done:
1039         return werr;
1040 }
1041
1042 static NTSTATUS cmd_netlogon_database_redo(struct rpc_pipe_client *cli,
1043                                            TALLOC_CTX *mem_ctx, int argc,
1044                                            const char **argv)
1045 {
1046         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1047         const char *server_name = cli->desthost;
1048         uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1049         struct netr_Authenticator clnt_creds, srv_cred;
1050         struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1051         unsigned char trust_passwd_hash[16];
1052         enum netr_SchannelType sec_channel_type = 0;
1053         struct netr_ChangeLogEntry e;
1054         uint32_t rid = 500;
1055
1056         if (argc > 2) {
1057                 fprintf(stderr, "Usage: %s <user rid>\n", argv[0]);
1058                 return NT_STATUS_OK;
1059         }
1060
1061         if (argc == 2) {
1062                 sscanf(argv[1], "%d", &rid);
1063         }
1064
1065         if (!secrets_fetch_trust_account_password(lp_workgroup(),
1066                                                   trust_passwd_hash,
1067                                                   NULL, &sec_channel_type)) {
1068                 return NT_STATUS_UNSUCCESSFUL;
1069         }
1070
1071         status = rpccli_netlogon_setup_creds(cli,
1072                                              server_name, /* server name */
1073                                              lp_workgroup(), /* domain */
1074                                              global_myname(), /* client name */
1075                                              global_myname(), /* machine account name */
1076                                              trust_passwd_hash,
1077                                              sec_channel_type,
1078                                              &neg_flags);
1079
1080         if (!NT_STATUS_IS_OK(status)) {
1081                 return status;
1082         }
1083
1084         netlogon_creds_client_authenticator(cli->dc, &clnt_creds);
1085
1086         ZERO_STRUCT(e);
1087
1088         e.object_rid            = rid;
1089         e.db_index              = SAM_DATABASE_DOMAIN;
1090         e.delta_type            = NETR_DELTA_USER;
1091
1092         status = rpccli_netr_DatabaseRedo(cli, mem_ctx,
1093                                           server_name,
1094                                           global_myname(),
1095                                           &clnt_creds,
1096                                           &srv_cred,
1097                                           e,
1098                                           0, /* is calculated automatically */
1099                                           &delta_enum_array);
1100
1101         if (!netlogon_creds_client_check(cli->dc, &srv_cred.cred)) {
1102                 DEBUG(0,("credentials chain check failed\n"));
1103                 return NT_STATUS_ACCESS_DENIED;
1104         }
1105
1106         return status;
1107 }
1108
1109 static NTSTATUS cmd_netlogon_capabilities(struct rpc_pipe_client *cli,
1110                                           TALLOC_CTX *mem_ctx, int argc,
1111                                           const char **argv)
1112 {
1113         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1114         struct netr_Authenticator credential;
1115         struct netr_Authenticator return_authenticator;
1116         union netr_Capabilities capabilities;
1117         uint32_t level = 1;
1118
1119         if (argc > 2) {
1120                 fprintf(stderr, "Usage: %s <level>\n", argv[0]);
1121                 return NT_STATUS_OK;
1122         }
1123
1124         if (argc == 2) {
1125                 level = atoi(argv[1]);
1126         }
1127
1128         ZERO_STRUCT(return_authenticator);
1129
1130         netlogon_creds_client_authenticator(cli->dc, &credential);
1131
1132         status = rpccli_netr_LogonGetCapabilities(cli, mem_ctx,
1133                                                   cli->desthost,
1134                                                   global_myname(),
1135                                                   &credential,
1136                                                   &return_authenticator,
1137                                                   level,
1138                                                   &capabilities);
1139
1140         if (!netlogon_creds_client_check(cli->dc,
1141                                          &return_authenticator.cred)) {
1142                 DEBUG(0,("credentials chain check failed\n"));
1143                 return NT_STATUS_ACCESS_DENIED;
1144         }
1145
1146         printf("capabilities: 0x%08x\n", capabilities.server_capabilities);
1147
1148         return status;
1149 }
1150
1151 /* List of commands exported by this module */
1152
1153 struct cmd_set netlogon_commands[] = {
1154
1155         { "NETLOGON" },
1156
1157         { "logonctrl2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl2, &ndr_table_netlogon.syntax_id, NULL, "Logon Control 2",     "" },
1158         { "getanydcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getanydcname, &ndr_table_netlogon.syntax_id, NULL, "Get trusted DC name",     "" },
1159         { "getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcname, &ndr_table_netlogon.syntax_id, NULL, "Get trusted PDC name",     "" },
1160         { "dsr_getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcname, &ndr_table_netlogon.syntax_id, NULL, "Get trusted DC name",     "" },
1161         { "dsr_getdcnameex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex, &ndr_table_netlogon.syntax_id, NULL, "Get trusted DC name",     "" },
1162         { "dsr_getdcnameex2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex2, &ndr_table_netlogon.syntax_id, NULL, "Get trusted DC name",     "" },
1163         { "dsr_getsitename", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getsitename, &ndr_table_netlogon.syntax_id, NULL, "Get sitename",     "" },
1164         { "dsr_getforesttrustinfo", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getforesttrustinfo, &ndr_table_netlogon.syntax_id, NULL, "Get Forest Trust Info",     "" },
1165         { "logonctrl",  RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl, &ndr_table_netlogon.syntax_id, NULL, "Logon Control",       "" },
1166         { "samsync",    RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_sync,    NULL, &ndr_table_netlogon.syntax_id, NULL, "Sam Synchronisation", "" },
1167         { "samdeltas",  RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_deltas,  NULL, &ndr_table_netlogon.syntax_id, NULL, "Query Sam Deltas",    "" },
1168         { "samlogon",   RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_logon,   NULL, &ndr_table_netlogon.syntax_id, NULL, "Sam Logon",           "" },
1169         { "change_trust_pw",   RPC_RTYPE_NTSTATUS, cmd_netlogon_change_trust_pw,   NULL, &ndr_table_netlogon.syntax_id, NULL, "Change Trust Account Password",           "" },
1170         { "gettrustrid", RPC_RTYPE_WERROR, NULL, cmd_netlogon_gettrustrid, &ndr_table_netlogon.syntax_id, NULL, "Get trust rid",     "" },
1171         { "dsr_enumtrustdom", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon.syntax_id, NULL, "Enumerate trusted domains",     "" },
1172         { "dsenumdomtrusts",  RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon.syntax_id, NULL, "Enumerate all trusted domains in an AD forest",     "" },
1173         { "deregisterdnsrecords", RPC_RTYPE_WERROR, NULL, cmd_netlogon_deregisterdnsrecords, &ndr_table_netlogon.syntax_id, NULL, "Deregister DNS records",     "" },
1174         { "netrenumtrusteddomains", RPC_RTYPE_NTSTATUS, cmd_netlogon_enumtrusteddomains, NULL, &ndr_table_netlogon.syntax_id, NULL, "Enumerate trusted domains",     "" },
1175         { "netrenumtrusteddomainsex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_enumtrusteddomainsex, &ndr_table_netlogon.syntax_id, NULL, "Enumerate trusted domains",     "" },
1176         { "getdcsitecoverage", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcsitecoverage, &ndr_table_netlogon.syntax_id, NULL, "Get the Site-Coverage from a DC",     "" },
1177         { "database_redo", RPC_RTYPE_NTSTATUS, cmd_netlogon_database_redo, NULL, &ndr_table_netlogon.syntax_id, NULL, "Replicate single object from a DC",     "" },
1178         { "capabilities", RPC_RTYPE_NTSTATUS, cmd_netlogon_capabilities, NULL, &ndr_table_netlogon.syntax_id, NULL, "Return Capabilities",     "" },
1179
1180         { NULL }
1181 };