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