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