3d73fc1fde98c6c36244cdba4fb422b035ba197f
[samba.git] / source3 / rpcclient / cmd_netlogon.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.2
4    RPC pipe client
5
6    Copyright (C) Tim Potter 2000
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 static NTSTATUS cmd_netlogon_logon_ctrl2(struct cli_state *cli, int argc,
26                                        char **argv)
27 {
28         uint32 query_level = 1;
29         TALLOC_CTX *mem_ctx;
30         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
31
32         if (argc > 1) {
33                 printf("Usage: %s\n", argv[0]);
34                 return NT_STATUS_OK;
35         }
36
37         if (!(mem_ctx = talloc_init())) {
38                 DEBUG(0,("cmd_srvsvc_srv_query_info: talloc_init failed\n"));
39                 goto done;
40         }
41
42         /* Initialise RPC connection */
43
44         if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
45                 DEBUG(0, ("Could not initialize srvsvc pipe!\n"));
46                 goto done;
47         }
48
49         result = cli_netlogon_logon_ctrl2(cli, mem_ctx, query_level);
50         if (!NT_STATUS_IS_OK(result)) {
51                 goto done;
52         }
53
54         /* Display results */
55
56  done:
57         return result;
58 }
59
60 static NTSTATUS cmd_netlogon_logon_ctrl(struct cli_state *cli, int argc,
61                                       char **argv)
62 {
63 #if 0
64         uint32 query_level = 1;
65 #endif
66         TALLOC_CTX *mem_ctx;
67         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
68
69         if (argc > 1) {
70                 printf("Usage: %s\n", argv[0]);
71                 return NT_STATUS_OK;
72         }
73
74         if (!(mem_ctx = talloc_init())) {
75                 DEBUG(0,("cmd_srvsvc_srv_query_info: talloc_init failed\n"));
76                 goto done;
77         }
78
79         /* Initialise RPC connection */
80
81         if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
82                 DEBUG(0, ("Could not initialize netlogon pipe!\n"));
83                 goto done;
84         }
85
86 #if 0
87         result = cli_netlogon_logon_ctrl(cli, mem_ctx, query_level);
88         if (!NT_STATUS_IS_OK(result)) {
89                 goto done;
90         }
91 #endif
92
93         /* Display results */
94
95  done:
96         return result;
97 }
98
99 /* Display sam synchronisation information */
100
101 static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas,
102                              SAM_DELTA_CTR *deltas)
103 {
104         fstring name;
105         uint32 i, j;
106
107         for (i = 0; i < num_deltas; i++) {
108                 switch (hdr_deltas[i].type) {
109                 case SAM_DELTA_DOMAIN_INFO:
110                         unistr2_to_ascii(name,
111                                          &deltas[i].domain_info.uni_dom_name,
112                                          sizeof(name) - 1);
113                         DEBUG(0, ("Domain: %s\n", name));
114                         break;
115                 case SAM_DELTA_GROUP_INFO:
116                         unistr2_to_ascii(name,
117                                          &deltas[i].group_info.uni_grp_name,
118                                          sizeof(name) - 1);
119                         DEBUG(0, ("Group: %s\n", name));
120                         break;
121                 case SAM_DELTA_ACCOUNT_INFO:
122                         unistr2_to_ascii(name, 
123                                          &deltas[i].account_info.uni_acct_name,
124                                          sizeof(name) - 1);
125                         DEBUG(0, ("Account: %s\n", name));
126                         break;
127                 case SAM_DELTA_ALIAS_INFO:
128                         unistr2_to_ascii(name, 
129                                          &deltas[i].alias_info.uni_als_name,
130                                          sizeof(name) - 1);
131                         DEBUG(0, ("Alias: %s\n", name));
132                         break;
133                 case SAM_DELTA_ALIAS_MEM: {
134                         SAM_ALIAS_MEM_INFO *alias = &deltas[i].als_mem_info;
135
136                         for (j = 0; j < alias->num_members; j++) {
137                                 fstring sid_str;
138
139                                 sid_to_string(sid_str, &alias->sids[j].sid);
140
141                                 DEBUG(0, ("%s\n", sid_str));
142                         }
143                         break;
144                 }
145                 case SAM_DELTA_GROUP_MEM: {
146                         SAM_GROUP_MEM_INFO *group = &deltas[i].grp_mem_info;
147
148                         for (j = 0; j < group->num_members; j++)
149                                 DEBUG(0, ("rid 0x%x, attrib 0x%08x\n", 
150                                           group->rids[j], group->attribs[j]));
151                         break;
152                 }
153                 case SAM_DELTA_SAM_STAMP: {
154                         SAM_DELTA_STAMP *stamp = &deltas[i].stamp;
155
156                         DEBUG(0, ("sam sequence update: 0x%04x\n",
157                                   stamp->seqnum));
158                         break;
159                 }                                  
160                 default:
161                         DEBUG(0, ("unknown delta type 0x%02x\n", 
162                                   hdr_deltas[i].type));
163                         break;
164                 }
165         }
166 }
167
168 /* Perform sam synchronisation */
169
170 static NTSTATUS cmd_netlogon_sam_sync(struct cli_state *cli, int argc,
171                                     char **argv)
172 {
173         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
174         unsigned char trust_passwd[16];
175         TALLOC_CTX *mem_ctx;
176         uint32 database_id = 0, num_deltas;
177         SAM_DELTA_HDR *hdr_deltas;
178         SAM_DELTA_CTR *deltas;
179
180         if (argc > 2) {
181                 printf("Usage: %s [database_id]\n", argv[0]);
182                 return NT_STATUS_OK;
183         }
184
185         if (argc == 2)
186                 database_id = atoi(argv[1]);
187
188         if (!secrets_init()) {
189                 DEBUG(0, ("Unable to initialise secrets database\n"));
190                 return result;
191         }
192
193         if (!(mem_ctx = talloc_init())) {
194                 DEBUG(0,("talloc_init failed\n"));
195                 return result;
196         }
197
198         /* Initialise RPC connection */
199
200         if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
201                 DEBUG(0, ("Could not initialize netlogon pipe!\n"));
202                 goto done;
203         }
204
205         /* Initialise session credentials */
206
207         if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd,
208                                                   NULL)) {
209                 DEBUG(0, ("could not fetch trust account password\n"));
210                 goto done;
211         }        
212
213         result = cli_nt_setup_creds(cli, trust_passwd);
214
215         if (!NT_STATUS_IS_OK(result)) {
216                 DEBUG(0, ("Error initialising session creds\n"));
217                 goto done;
218         }
219
220         /* Synchronise sam database */
221
222         result = cli_netlogon_sam_sync(cli, mem_ctx, database_id,
223                                        &num_deltas, &hdr_deltas, &deltas);
224         if (!NT_STATUS_IS_OK(result)) {
225                 goto done;
226         }
227
228         /* Display results */
229
230         display_sam_sync(num_deltas, hdr_deltas, deltas);
231
232  done:
233         cli_nt_session_close(cli);
234         talloc_destroy(mem_ctx);
235         
236         return result;
237 }
238
239 /* Perform sam delta synchronisation */
240
241 static NTSTATUS cmd_netlogon_sam_deltas(struct cli_state *cli, int argc,
242                                       char **argv)
243 {
244         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
245         unsigned char trust_passwd[16];
246         TALLOC_CTX *mem_ctx = NULL;
247         uint32 database_id, num_deltas, tmp;
248         SAM_DELTA_HDR *hdr_deltas;
249         SAM_DELTA_CTR *deltas;
250         UINT64_S seqnum;
251
252         if (argc != 3) {
253                 printf("Usage: %s database_id seqnum\n", argv[0]);
254                 return NT_STATUS_OK;
255         }
256
257         database_id = atoi(argv[1]);
258         tmp = atoi(argv[2]);
259
260         seqnum.low = tmp & 0xffff;
261         seqnum.high = 0;
262
263         if (!secrets_init()) {
264                 DEBUG(0, ("Unable to initialise secrets database\n"));
265                 goto done;
266         }
267
268         if (!(mem_ctx = talloc_init())) {
269                 DEBUG(0,("talloc_init failed\n"));
270                 goto done;
271         }
272
273         /* Initialise RPC connection */
274
275         if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
276                 DEBUG(0, ("Could not initialize netlogon pipe!\n"));
277                 goto done;
278         }
279
280         /* Initialise session credentials */
281
282         if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd,
283                                                   NULL)) {
284                 DEBUG(0, ("could not fetch trust account password\n"));
285                 goto done;
286         }        
287
288         result = cli_nt_setup_creds(cli, trust_passwd);
289
290         if (!NT_STATUS_IS_OK(result)) {
291                 DEBUG(0, ("Error initialising session creds\n"));
292                 goto done;
293         }
294
295         /* Synchronise sam database */
296
297         result = cli_netlogon_sam_deltas(cli, mem_ctx, database_id,
298                                          seqnum, &num_deltas, 
299                                          &hdr_deltas, &deltas);
300         if (!NT_STATUS_IS_OK(result)) {
301                 goto done;
302         }
303
304         /* Display results */
305
306         display_sam_sync(num_deltas, hdr_deltas, deltas);
307         
308  done:
309         cli_nt_session_close(cli);
310         talloc_destroy(mem_ctx);
311
312         return result;
313 }
314
315 /* List of commands exported by this module */
316
317 struct cmd_set netlogon_commands[] = {
318
319         { "NETLOGON" },
320
321         { "logonctrl2", cmd_netlogon_logon_ctrl2, "Logon Control 2",     "" },
322         { "logonctrl",  cmd_netlogon_logon_ctrl,  "Logon Control",       "" },
323         { "samsync",    cmd_netlogon_sam_sync,    "Sam Synchronisation", "" },
324         { "samdeltas",  cmd_netlogon_sam_deltas,  "Query Sam Deltas", "" },
325
326         { NULL }
327 };