r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[tprouty/samba.git] / source / rpcclient / cmd_netlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4
5    Copyright (C) Tim Potter 2000
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "rpcclient.h"
23
24 static NTSTATUS cmd_netlogon_logon_ctrl2(struct rpc_pipe_client *cli, 
25                                          TALLOC_CTX *mem_ctx, int argc, 
26                                          const char **argv)
27 {
28         uint32 query_level = 1;
29         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
30
31         if (argc > 1) {
32                 fprintf(stderr, "Usage: %s\n", argv[0]);
33                 return NT_STATUS_OK;
34         }
35
36         result = rpccli_netlogon_logon_ctrl2(cli, mem_ctx, query_level);
37
38         if (!NT_STATUS_IS_OK(result))
39                 goto done;
40
41         /* Display results */
42
43  done:
44         return result;
45 }
46
47 static WERROR cmd_netlogon_getanydcname(struct rpc_pipe_client *cli, 
48                                         TALLOC_CTX *mem_ctx, int argc, 
49                                         const char **argv)
50 {
51         fstring dcname;
52         WERROR result = WERR_GENERAL_FAILURE;
53         int old_timeout;
54
55         if (argc != 2) {
56                 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
57                 return WERR_OK;
58         }
59
60         /* Make sure to wait for our DC's reply */
61         old_timeout = cli_set_timeout(cli->cli, MAX(cli->cli->timeout,30000)); /* 30 seconds. */
62
63         result = rpccli_netlogon_getanydcname(cli, mem_ctx, cli->cli->desthost, argv[1], dcname);
64
65         cli_set_timeout(cli->cli, old_timeout);
66
67         if (!W_ERROR_IS_OK(result))
68                 goto done;
69
70         /* Display results */
71
72         printf("%s\n", dcname);
73
74  done:
75         return result;
76 }
77
78 static WERROR cmd_netlogon_getdcname(struct rpc_pipe_client *cli, 
79                                      TALLOC_CTX *mem_ctx, int argc, 
80                                      const char **argv)
81 {
82         fstring dcname;
83         WERROR result = WERR_GENERAL_FAILURE;
84         int old_timeout;
85
86         if (argc != 2) {
87                 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
88                 return WERR_OK;
89         }
90
91         /* Make sure to wait for our DC's reply */
92         old_timeout = cli_set_timeout(cli->cli, MAX(cli->cli->timeout,30000)); /* 30 seconds. */
93
94         result = rpccli_netlogon_getdcname(cli, mem_ctx, cli->cli->desthost, argv[1], dcname);
95
96         cli_set_timeout(cli->cli, old_timeout);
97
98         if (!W_ERROR_IS_OK(result))
99                 goto done;
100
101         /* Display results */
102
103         printf("%s\n", dcname);
104
105  done:
106         return result;
107 }
108
109 static void display_ds_domain_controller_info(TALLOC_CTX *mem_ctx, const struct DS_DOMAIN_CONTROLLER_INFO *info)
110 {
111         d_printf("domain_controller_name: %s\n", info->domain_controller_name);
112         d_printf("domain_controller_address: %s\n", info->domain_controller_address);
113         d_printf("domain_controller_address_type: %d\n", info->domain_controller_address_type);
114         d_printf("domain_guid: %s\n", GUID_string(mem_ctx, info->domain_guid));
115         d_printf("domain_name: %s\n", info->domain_name);
116         d_printf("dns_forest_name: %s\n", info->dns_forest_name);
117         d_printf("flags: 0x%08x\n"
118                  "\tIs a PDC:                                   %s\n"
119                  "\tIs a GC of the forest:                      %s\n"
120                  "\tIs an LDAP server:                          %s\n"
121                  "\tSupports DS:                                %s\n"
122                  "\tIs running a KDC:                           %s\n"
123                  "\tIs running time services:                   %s\n"
124                  "\tIs the closest DC:                          %s\n"
125                  "\tIs writable:                                %s\n"
126                  "\tHas a hardware clock:                       %s\n"
127                  "\tIs a non-domain NC serviced by LDAP server: %s\n"
128                  "\tDomainControllerName is a DNS name:         %s\n"
129                  "\tDomainName is a DNS name:                   %s\n"
130                  "\tDnsForestName is a DNS name:                %s\n",
131                  info->flags,
132                  (info->flags & ADS_PDC) ? "yes" : "no",
133                  (info->flags & ADS_GC) ? "yes" : "no",
134                  (info->flags & ADS_LDAP) ? "yes" : "no",
135                  (info->flags & ADS_DS) ? "yes" : "no",
136                  (info->flags & ADS_KDC) ? "yes" : "no",
137                  (info->flags & ADS_TIMESERV) ? "yes" : "no",
138                  (info->flags & ADS_CLOSEST) ? "yes" : "no",
139                  (info->flags & ADS_WRITABLE) ? "yes" : "no",
140                  (info->flags & ADS_GOOD_TIMESERV) ? "yes" : "no",
141                  (info->flags & ADS_NDNC) ? "yes" : "no",
142                  (info->flags & ADS_DNS_CONTROLLER) ? "yes":"no",
143                  (info->flags & ADS_DNS_DOMAIN) ? "yes":"no",
144                  (info->flags & ADS_DNS_FOREST) ? "yes":"no");
145
146         d_printf("dc_site_name: %s\n", info->dc_site_name);
147         d_printf("client_site_name: %s\n", info->client_site_name);
148 }
149
150 static WERROR cmd_netlogon_dsr_getdcname(struct rpc_pipe_client *cli,
151                                          TALLOC_CTX *mem_ctx, int argc,
152                                          const char **argv)
153 {
154         WERROR result;
155         uint32 flags = DS_RETURN_DNS_NAME;
156         const char *server_name = cli->cli->desthost;
157         const char *domain_name;
158         struct GUID domain_guid = GUID_zero();
159         struct GUID site_guid = GUID_zero();
160         struct DS_DOMAIN_CONTROLLER_INFO *info = NULL;
161
162         if (argc < 2) {
163                 fprintf(stderr, "Usage: %s [domainname] [domain_name] [domain_guid] [site_guid] [flags]\n", argv[0]);
164                 return WERR_OK;
165         }
166
167         if (argc >= 2)
168                 domain_name = argv[1];
169
170         if (argc >= 3) {
171                 if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
172                         return WERR_NOMEM;
173                 }
174         }
175
176         if (argc >= 4) {
177                 if (!NT_STATUS_IS_OK(GUID_from_string(argv[3], &site_guid))) {
178                         return WERR_NOMEM;
179                 }
180         }
181
182         if (argc >= 5)
183                 sscanf(argv[4], "%x", &flags);
184         
185         result = rpccli_netlogon_dsr_getdcname(cli, mem_ctx, server_name, domain_name, 
186                                                &domain_guid, &site_guid, flags,
187                                                &info);
188
189         if (W_ERROR_IS_OK(result)) {
190                 d_printf("DsGetDcName gave\n");
191                 display_ds_domain_controller_info(mem_ctx, info);
192                 return WERR_OK;
193         }
194
195         printf("rpccli_netlogon_dsr_getdcname returned %s\n",
196                dos_errstr(result));
197
198         return result;
199 }
200
201 static WERROR cmd_netlogon_dsr_getdcnameex(struct rpc_pipe_client *cli,
202                                            TALLOC_CTX *mem_ctx, int argc,
203                                            const char **argv)
204 {
205         WERROR result;
206         uint32 flags = DS_RETURN_DNS_NAME;
207         const char *server_name = cli->cli->desthost;
208         const char *domain_name;
209         const char *site_name = NULL;
210         struct GUID domain_guid = GUID_zero();
211         struct DS_DOMAIN_CONTROLLER_INFO *info = NULL;
212
213         if (argc < 2) {
214                 fprintf(stderr, "Usage: %s [domainname] [domain_name] [domain_guid] [site_name] [flags]\n", argv[0]);
215                 return WERR_OK;
216         }
217
218         if (argc >= 2)
219                 domain_name = argv[1];
220
221         if (argc >= 3) {
222                 if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
223                         return WERR_NOMEM;
224                 }
225         }
226
227         if (argc >= 4)
228                 site_name = argv[3];
229
230         if (argc >= 5)
231                 sscanf(argv[4], "%x", &flags);
232
233         result = rpccli_netlogon_dsr_getdcnameex(cli, mem_ctx, server_name, domain_name, 
234                                                  &domain_guid, site_name, flags,
235                                                  &info);
236
237         if (W_ERROR_IS_OK(result)) {
238                 d_printf("DsGetDcNameEx gave\n");
239                 display_ds_domain_controller_info(mem_ctx, info);
240                 return WERR_OK;
241         }
242
243         printf("rpccli_netlogon_dsr_getdcnameex returned %s\n",
244                dos_errstr(result));
245
246         return result;
247 }
248
249 static WERROR cmd_netlogon_dsr_getdcnameex2(struct rpc_pipe_client *cli,
250                                             TALLOC_CTX *mem_ctx, int argc,
251                                             const char **argv)
252 {
253         WERROR result;
254         uint32 flags = DS_RETURN_DNS_NAME;
255         const char *server_name = cli->cli->desthost;
256         const char *domain_name = NULL;
257         const char *client_account = NULL;
258         uint32 mask = 0;
259         const char *site_name = NULL;
260         struct GUID domain_guid = GUID_zero();
261         struct DS_DOMAIN_CONTROLLER_INFO *info = NULL;
262
263         if (argc < 2) {
264                 fprintf(stderr, "Usage: %s [domainname] [client_account] [acb_mask] [domain_name] [domain_guid] [site_name] [flags]\n", argv[0]);
265                 return WERR_OK;
266         }
267
268         if (argc >= 2)
269                 client_account = argv[1];
270
271         if (argc >= 3)
272                 mask = atoi(argv[2]);
273         
274         if (argc >= 4)
275                 domain_name = argv[3];
276
277         if (argc >= 5) {
278                 if (!NT_STATUS_IS_OK(GUID_from_string(argv[4], &domain_guid))) {
279                         return WERR_NOMEM;
280                 }
281         }
282
283         if (argc >= 6)
284                 site_name = argv[5];
285
286         if (argc >= 7)
287                 sscanf(argv[6], "%x", &flags);
288
289         result = rpccli_netlogon_dsr_getdcnameex2(cli, mem_ctx, server_name, 
290                                                   client_account, mask,
291                                                   domain_name, &domain_guid,
292                                                   site_name, flags,
293                                                   &info);
294
295         if (W_ERROR_IS_OK(result)) {
296                 d_printf("DsGetDcNameEx2 gave\n");
297                 display_ds_domain_controller_info(mem_ctx, info);
298                 return WERR_OK;
299         }
300
301         printf("rpccli_netlogon_dsr_getdcnameex2 returned %s\n",
302                dos_errstr(result));
303
304         return result;
305 }
306
307
308 static WERROR cmd_netlogon_dsr_getsitename(struct rpc_pipe_client *cli,
309                                            TALLOC_CTX *mem_ctx, int argc,
310                                            const char **argv)
311 {
312         WERROR result;
313         char *sitename;
314
315         if (argc != 2) {
316                 fprintf(stderr, "Usage: %s computername\n", argv[0]);
317                 return WERR_OK;
318         }
319
320         result = rpccli_netlogon_dsr_getsitename(cli, mem_ctx, argv[1], &sitename);
321
322         if (!W_ERROR_IS_OK(result)) {
323                 printf("rpccli_netlogon_dsr_gesitename returned %s\n",
324                        nt_errstr(werror_to_ntstatus(result)));
325                 return result;
326         }
327
328         printf("Computer %s is on Site: %s\n", argv[1], sitename);
329
330         return WERR_OK;
331 }
332
333 static NTSTATUS cmd_netlogon_logon_ctrl(struct rpc_pipe_client *cli, 
334                                         TALLOC_CTX *mem_ctx, int argc, 
335                                         const char **argv)
336 {
337 #if 0
338         uint32 query_level = 1;
339 #endif
340         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
341
342         if (argc > 1) {
343                 fprintf(stderr, "Usage: %s\n", argv[0]);
344                 return NT_STATUS_OK;
345         }
346
347 #if 0
348         result = cli_netlogon_logon_ctrl(cli, mem_ctx, query_level);
349         if (!NT_STATUS_IS_OK(result)) {
350                 goto done;
351         }
352 #endif
353
354         /* Display results */
355
356         return result;
357 }
358
359 /* Display sam synchronisation information */
360
361 static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas,
362                              SAM_DELTA_CTR *deltas)
363 {
364         fstring name;
365         uint32 i, j;
366
367         for (i = 0; i < num_deltas; i++) {
368                 switch (hdr_deltas[i].type) {
369                 case SAM_DELTA_DOMAIN_INFO:
370                         unistr2_to_ascii(name,
371                                          &deltas[i].domain_info.uni_dom_name,
372                                          sizeof(name) - 1);
373                         printf("Domain: %s\n", name);
374                         break;
375                 case SAM_DELTA_GROUP_INFO:
376                         unistr2_to_ascii(name,
377                                          &deltas[i].group_info.uni_grp_name,
378                                          sizeof(name) - 1);
379                         printf("Group: %s\n", name);
380                         break;
381                 case SAM_DELTA_ACCOUNT_INFO:
382                         unistr2_to_ascii(name, 
383                                          &deltas[i].account_info.uni_acct_name,
384                                          sizeof(name) - 1);
385                         printf("Account: %s\n", name);
386                         break;
387                 case SAM_DELTA_ALIAS_INFO:
388                         unistr2_to_ascii(name, 
389                                          &deltas[i].alias_info.uni_als_name,
390                                          sizeof(name) - 1);
391                         printf("Alias: %s\n", name);
392                         break;
393                 case SAM_DELTA_ALIAS_MEM: {
394                         SAM_ALIAS_MEM_INFO *alias = &deltas[i].als_mem_info;
395
396                         for (j = 0; j < alias->num_members; j++) {
397                                 fstring sid_str;
398
399                                 sid_to_string(sid_str, &alias->sids[j].sid);
400
401                                 printf("%s\n", sid_str);
402                         }
403                         break;
404                 }
405                 case SAM_DELTA_GROUP_MEM: {
406                         SAM_GROUP_MEM_INFO *group = &deltas[i].grp_mem_info;
407
408                         for (j = 0; j < group->num_members; j++)
409                                 printf("rid 0x%x, attrib 0x%08x\n", 
410                                           group->rids[j], group->attribs[j]);
411                         break;
412                 }
413                 case SAM_DELTA_MODIFIED_COUNT: {
414                         SAM_DELTA_MOD_COUNT *mc = &deltas[i].mod_count;
415
416                         printf("sam sequence update: 0x%04x\n", mc->seqnum);
417                         break;
418                 }                                  
419                 default:
420                         printf("unknown delta type 0x%02x\n", 
421                                   hdr_deltas[i].type);
422                         break;
423                 }
424         }
425 }
426
427 /* Perform sam synchronisation */
428
429 static NTSTATUS cmd_netlogon_sam_sync(struct rpc_pipe_client *cli, 
430                                       TALLOC_CTX *mem_ctx, int argc,
431                                       const char **argv)
432 {
433         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
434         uint32 database_id = 0, num_deltas;
435         SAM_DELTA_HDR *hdr_deltas;
436         SAM_DELTA_CTR *deltas;
437
438         if (argc > 2) {
439                 fprintf(stderr, "Usage: %s [database_id]\n", argv[0]);
440                 return NT_STATUS_OK;
441         }
442
443         if (argc == 2)
444                 database_id = atoi(argv[1]);
445
446         /* Synchronise sam database */
447
448         result = rpccli_netlogon_sam_sync(cli, mem_ctx, database_id,
449                                        0, &num_deltas, &hdr_deltas, &deltas);
450
451         if (!NT_STATUS_IS_OK(result))
452                 goto done;
453
454         /* Display results */
455
456         display_sam_sync(num_deltas, hdr_deltas, deltas);
457
458  done:
459         return result;
460 }
461
462 /* Perform sam delta synchronisation */
463
464 static NTSTATUS cmd_netlogon_sam_deltas(struct rpc_pipe_client *cli, 
465                                         TALLOC_CTX *mem_ctx, int argc,
466                                         const char **argv)
467 {
468         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
469         uint32 database_id, num_deltas, tmp;
470         SAM_DELTA_HDR *hdr_deltas;
471         SAM_DELTA_CTR *deltas;
472         uint64 seqnum;
473
474         if (argc != 3) {
475                 fprintf(stderr, "Usage: %s database_id seqnum\n", argv[0]);
476                 return NT_STATUS_OK;
477         }
478
479         database_id = atoi(argv[1]);
480         tmp = atoi(argv[2]);
481
482         seqnum = tmp & 0xffff;
483
484         result = rpccli_netlogon_sam_deltas(cli, mem_ctx, database_id,
485                                          seqnum, &num_deltas, 
486                                          &hdr_deltas, &deltas);
487
488         if (!NT_STATUS_IS_OK(result))
489                 goto done;
490
491         /* Display results */
492
493         display_sam_sync(num_deltas, hdr_deltas, deltas);
494         
495  done:
496         return result;
497 }
498
499 /* Log on a domain user */
500
501 static NTSTATUS cmd_netlogon_sam_logon(struct rpc_pipe_client *cli, 
502                                        TALLOC_CTX *mem_ctx, int argc,
503                                        const char **argv)
504 {
505         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
506         int logon_type = NET_LOGON_TYPE;
507         const char *username, *password;
508         int auth_level = 2;
509         uint32 logon_param = 0;
510         const char *workstation = NULL;
511
512         /* Check arguments */
513
514         if (argc < 3 || argc > 7) {
515                 fprintf(stderr, "Usage: samlogon <username> <password> [workstation]"
516                         "[logon_type (1 or 2)] [auth level (2 or 3)] [logon_parameter]\n");
517                 return NT_STATUS_OK;
518         }
519
520         username = argv[1];
521         password = argv[2];
522
523         if (argc >= 4) 
524                 workstation = argv[3];
525
526         if (argc >= 5)
527                 sscanf(argv[4], "%i", &logon_type);
528
529         if (argc >= 6)
530                 sscanf(argv[5], "%i", &auth_level);
531
532         if (argc == 7)
533                 sscanf(argv[6], "%x", &logon_param);
534
535         /* Perform the sam logon */
536
537         result = rpccli_netlogon_sam_logon(cli, mem_ctx, logon_param, lp_workgroup(), username, password, workstation, logon_type);
538
539         if (!NT_STATUS_IS_OK(result))
540                 goto done;
541
542  done:
543         return result;
544 }
545
546 /* Change the trust account password */
547
548 static NTSTATUS cmd_netlogon_change_trust_pw(struct rpc_pipe_client *cli, 
549                                              TALLOC_CTX *mem_ctx, int argc,
550                                              const char **argv)
551 {
552         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
553
554         /* Check arguments */
555
556         if (argc > 1) {
557                 fprintf(stderr, "Usage: change_trust_pw");
558                 return NT_STATUS_OK;
559         }
560
561         /* Perform the sam logon */
562
563         result = trust_pw_find_change_and_store_it(cli, mem_ctx,
564                                                    lp_workgroup());
565
566         if (!NT_STATUS_IS_OK(result))
567                 goto done;
568
569  done:
570         return result;
571 }
572
573
574 /* List of commands exported by this module */
575
576 struct cmd_set netlogon_commands[] = {
577
578         { "NETLOGON" },
579
580         { "logonctrl2", RPC_RTYPE_NTSTATUS, cmd_netlogon_logon_ctrl2, NULL, PI_NETLOGON, NULL, "Logon Control 2",     "" },
581         { "getanydcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getanydcname, PI_NETLOGON, NULL, "Get trusted DC name",     "" },
582         { "getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcname, PI_NETLOGON, NULL, "Get trusted PDC name",     "" },
583         { "dsr_getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcname, PI_NETLOGON, NULL, "Get trusted DC name",     "" },
584         { "dsr_getdcnameex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex, PI_NETLOGON, NULL, "Get trusted DC name",     "" },
585         { "dsr_getdcnameex2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex2, PI_NETLOGON, NULL, "Get trusted DC name",     "" },
586         { "dsr_getsitename", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getsitename, PI_NETLOGON, NULL, "Get sitename",     "" },
587         { "logonctrl",  RPC_RTYPE_NTSTATUS, cmd_netlogon_logon_ctrl,  NULL, PI_NETLOGON, NULL, "Logon Control",       "" },
588         { "samsync",    RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_sync,    NULL, PI_NETLOGON, NULL, "Sam Synchronisation", "" },
589         { "samdeltas",  RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_deltas,  NULL, PI_NETLOGON, NULL, "Query Sam Deltas",    "" },
590         { "samlogon",   RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_logon,   NULL, PI_NETLOGON, NULL, "Sam Logon",           "" },
591         { "change_trust_pw",   RPC_RTYPE_NTSTATUS, cmd_netlogon_change_trust_pw,   NULL, PI_NETLOGON, NULL, "Change Trust Account Password",           "" },
592
593         { NULL }
594 };