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