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