r10656: BIG merge from trunk. Features not copied over
[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 rpc_pipe_client *cli, 
26                                          TALLOC_CTX *mem_ctx, int argc, 
27                                          const 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 = rpccli_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_getdcname(struct rpc_pipe_client *cli, 
49                                        TALLOC_CTX *mem_ctx, int argc, 
50                                        const char **argv)
51 {
52         fstring dcname;
53         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
54
55         if (argc != 2) {
56                 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
57                 return NT_STATUS_OK;
58         }
59
60         result = rpccli_netlogon_getdcname(cli, mem_ctx, cli->cli->desthost, argv[1], dcname);
61
62         if (!NT_STATUS_IS_OK(result))
63                 goto done;
64
65         /* Display results */
66
67         printf("%s\n", dcname);
68
69  done:
70         return result;
71 }
72
73 static NTSTATUS cmd_netlogon_logon_ctrl(struct rpc_pipe_client *cli, 
74                                         TALLOC_CTX *mem_ctx, int argc, 
75                                         const char **argv)
76 {
77 #if 0
78         uint32 query_level = 1;
79 #endif
80         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
81
82         if (argc > 1) {
83                 fprintf(stderr, "Usage: %s\n", argv[0]);
84                 return NT_STATUS_OK;
85         }
86
87 #if 0
88         result = cli_netlogon_logon_ctrl(cli, mem_ctx, query_level);
89         if (!NT_STATUS_IS_OK(result)) {
90                 goto done;
91         }
92 #endif
93
94         /* Display results */
95
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                         printf("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                         printf("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                         printf("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                         printf("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                                 printf("%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                                 printf("rid 0x%x, attrib 0x%08x\n", 
150                                           group->rids[j], group->attribs[j]);
151                         break;
152                 }
153                 case SAM_DELTA_MODIFIED_COUNT: {
154                         SAM_DELTA_MOD_COUNT *mc = &deltas[i].mod_count;
155
156                         printf("sam sequence update: 0x%04x\n", mc->seqnum);
157                         break;
158                 }                                  
159                 default:
160                         printf("unknown delta type 0x%02x\n", 
161                                   hdr_deltas[i].type);
162                         break;
163                 }
164         }
165 }
166
167 /* Perform sam synchronisation */
168
169 static NTSTATUS cmd_netlogon_sam_sync(struct rpc_pipe_client *cli, 
170                                       TALLOC_CTX *mem_ctx, int argc,
171                                       const char **argv)
172 {
173         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
174         uint32 database_id = 0, num_deltas;
175         SAM_DELTA_HDR *hdr_deltas;
176         SAM_DELTA_CTR *deltas;
177
178         if (argc > 2) {
179                 fprintf(stderr, "Usage: %s [database_id]\n", argv[0]);
180                 return NT_STATUS_OK;
181         }
182
183         if (argc == 2)
184                 database_id = atoi(argv[1]);
185
186         /* Synchronise sam database */
187
188         result = rpccli_netlogon_sam_sync(cli, mem_ctx, database_id,
189                                        0, &num_deltas, &hdr_deltas, &deltas);
190
191         if (!NT_STATUS_IS_OK(result))
192                 goto done;
193
194         /* Display results */
195
196         display_sam_sync(num_deltas, hdr_deltas, deltas);
197
198  done:
199         return result;
200 }
201
202 /* Perform sam delta synchronisation */
203
204 static NTSTATUS cmd_netlogon_sam_deltas(struct rpc_pipe_client *cli, 
205                                         TALLOC_CTX *mem_ctx, int argc,
206                                         const char **argv)
207 {
208         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
209         uint32 database_id, num_deltas, tmp;
210         SAM_DELTA_HDR *hdr_deltas;
211         SAM_DELTA_CTR *deltas;
212         UINT64_S seqnum;
213
214         if (argc != 3) {
215                 fprintf(stderr, "Usage: %s database_id seqnum\n", argv[0]);
216                 return NT_STATUS_OK;
217         }
218
219         database_id = atoi(argv[1]);
220         tmp = atoi(argv[2]);
221
222         seqnum.low = tmp & 0xffff;
223         seqnum.high = 0;
224
225         result = rpccli_netlogon_sam_deltas(cli, mem_ctx, database_id,
226                                          seqnum, &num_deltas, 
227                                          &hdr_deltas, &deltas);
228
229         if (!NT_STATUS_IS_OK(result))
230                 goto done;
231
232         /* Display results */
233
234         display_sam_sync(num_deltas, hdr_deltas, deltas);
235         
236  done:
237         return result;
238 }
239
240 /* Log on a domain user */
241
242 static NTSTATUS cmd_netlogon_sam_logon(struct rpc_pipe_client *cli, 
243                                        TALLOC_CTX *mem_ctx, int argc,
244                                        const char **argv)
245 {
246         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
247         int logon_type = NET_LOGON_TYPE;
248         const char *username, *password;
249         uint32 neg_flags = 0x000001ff;
250         int auth_level = 2;
251
252         /* Check arguments */
253
254         if (argc < 3 || argc > 6) {
255                 fprintf(stderr, "Usage: samlogon <username> <password> "
256                         "[logon_type] [neg flags] [auth level (2 or 3)]\n"
257                         "neg flags being 0x000001ff or 0x6007ffff\n");
258                 return NT_STATUS_OK;
259         }
260
261         username = argv[1];
262         password = argv[2];
263
264         if (argc >= 4)
265                 sscanf(argv[3], "%i", &logon_type);
266
267         if (argc >= 5)
268                 sscanf(argv[4], "%i", &neg_flags);
269
270         if (argc == 6)
271                 sscanf(argv[5], "%i", &auth_level);
272
273         /* Perform the sam logon */
274
275         result = rpccli_netlogon_sam_logon(cli, mem_ctx, lp_workgroup(), username, password, logon_type);
276
277         if (!NT_STATUS_IS_OK(result))
278                 goto done;
279
280  done:
281         return result;
282 }
283
284 /* Change the trust account password */
285
286 static NTSTATUS cmd_netlogon_change_trust_pw(struct rpc_pipe_client *cli, 
287                                              TALLOC_CTX *mem_ctx, int argc,
288                                              const char **argv)
289 {
290         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
291
292         /* Check arguments */
293
294         if (argc > 1) {
295                 fprintf(stderr, "Usage: change_trust_pw");
296                 return NT_STATUS_OK;
297         }
298
299         /* Perform the sam logon */
300
301         result = trust_pw_find_change_and_store_it(cli, mem_ctx,
302                                                    lp_workgroup());
303
304         if (!NT_STATUS_IS_OK(result))
305                 goto done;
306
307  done:
308         return result;
309 }
310
311
312 /* List of commands exported by this module */
313
314 struct cmd_set netlogon_commands[] = {
315
316         { "NETLOGON" },
317
318         { "logonctrl2", RPC_RTYPE_NTSTATUS, cmd_netlogon_logon_ctrl2, NULL, PI_NETLOGON, NULL, "Logon Control 2",     "" },
319         { "getdcname", RPC_RTYPE_NTSTATUS, cmd_netlogon_getdcname, NULL, PI_NETLOGON, NULL, "Get trusted DC name",     "" },
320         { "logonctrl",  RPC_RTYPE_NTSTATUS, cmd_netlogon_logon_ctrl,  NULL, PI_NETLOGON, NULL, "Logon Control",       "" },
321         { "samsync",    RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_sync,    NULL, PI_NETLOGON, NULL, "Sam Synchronisation", "" },
322         { "samdeltas",  RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_deltas,  NULL, PI_NETLOGON, NULL, "Query Sam Deltas",    "" },
323         { "samlogon",   RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_logon,   NULL, PI_NETLOGON, NULL, "Sam Logon",           "" },
324         { "change_trust_pw",   RPC_RTYPE_NTSTATUS, cmd_netlogon_change_trust_pw,   NULL, PI_NETLOGON, NULL, "Change Trust Account Password",           "" },
325
326         { NULL }
327 };