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