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