Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[nivanova/samba-autobuild/.git] / source3 / rpcclient / cmd_lsarpc.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 extern pstring server;
26
27 /* Look up domain related information on a remote host */
28 static NTSTATUS cmd_lsa_query_info_policy(struct cli_state *cli, int argc, char **argv) 
29 {
30         POLICY_HND pol;
31         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
32         BOOL got_policy_hnd = False;
33         DOM_SID dom_sid;
34         fstring sid_str, domain_name;
35         uint32 info_class = 3;
36         TALLOC_CTX *mem_ctx;
37
38         if (argc > 2) {
39                 printf("Usage: %s [info_class]\n", argv[0]);
40                 return NT_STATUS_OK;
41         }
42
43         if (!(mem_ctx = talloc_init())) {
44                 DEBUG(0,("cmd_lsa_query_info_poicy: talloc_init failed\n"));
45                 return NT_STATUS_UNSUCCESSFUL;
46         }
47
48         if (argc == 2) {
49                 info_class = atoi(argv[1]);
50         }
51         
52         /* Initialise RPC connection */
53         if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
54                 DEBUG(0, ("Could not initialize samr pipe!\n"));
55                 talloc_destroy(mem_ctx);
56                 return NT_STATUS_UNSUCCESSFUL;
57         }
58
59         result = cli_lsa_open_policy(cli, mem_ctx, True, 
60                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
61                                      &pol);
62         if (!NT_STATUS_IS_OK(result)) {
63                 goto done;
64         }
65
66         got_policy_hnd = True;
67
68         /* Lookup info policy */
69
70         result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class, 
71                                            domain_name, &dom_sid);
72         if (!NT_STATUS_IS_OK(result)) {
73                 goto done;
74         }
75
76         sid_to_string(sid_str, &dom_sid);
77
78         if (domain_name[0]) {
79                 printf("domain %s has sid %s\n", domain_name, sid_str);
80         } else {
81                 printf("could not query info for level %d\n", info_class);
82         }
83
84 done:
85
86         if (got_policy_hnd) {
87                 cli_lsa_close(cli, mem_ctx, &pol);
88         }
89
90         cli_nt_session_close(cli);
91         talloc_destroy(mem_ctx);
92
93         return result;
94 }
95
96 /* Resolve a list of names to a list of sids */
97
98 static NTSTATUS cmd_lsa_lookup_names(struct cli_state *cli, int argc, char **argv)
99 {
100         POLICY_HND pol;
101         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
102         BOOL got_policy_hnd = False;
103         DOM_SID *sids;
104         uint32 *types;
105         int num_names, i;
106         TALLOC_CTX *mem_ctx;
107
108         if (argc == 1) {
109                 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
110                 return NT_STATUS_OK;
111         }
112
113         if (!(mem_ctx = talloc_init())) {
114                 DEBUG(0,("cmd_lsa_lookup_names: talloc_init failed\n"));
115                 return NT_STATUS_UNSUCCESSFUL;
116         }
117
118         /* Initialise RPC connection */
119         if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
120                 DEBUG(0, ("Could not initialize samr pipe!\n"));
121                 talloc_destroy(mem_ctx);
122                 return NT_STATUS_UNSUCCESSFUL;
123         }
124
125
126         result = cli_lsa_open_policy(cli, mem_ctx, True, 
127                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
128                                      &pol);
129         if (!NT_STATUS_IS_OK(result)) {
130                 goto done;
131         }
132
133         got_policy_hnd = True;
134
135         /* Lookup the names */
136
137         result = cli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1, 
138                                       &argv[1], &sids, &types, &num_names);
139         if (!NT_STATUS_IS_OK(result)) {
140                 goto done;
141         }
142
143         /* Print results */
144
145         for (i = 0; i < num_names; i++) {
146                 fstring sid_str;
147
148                 sid_to_string(sid_str, &sids[i]);
149                 printf("%s %s (%d)\n", argv[i + 1], sid_str,
150                        types[i]);
151         }
152
153  done:
154
155         if (got_policy_hnd) {
156                 cli_lsa_close(cli, mem_ctx, &pol);
157         }
158
159         cli_nt_session_close(cli);
160         talloc_destroy(mem_ctx);
161
162         return result;
163 }
164
165 /* Resolve a list of SIDs to a list of names */
166
167 static NTSTATUS cmd_lsa_lookup_sids(struct cli_state *cli, int argc, char **argv)
168 {
169         POLICY_HND pol;
170         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
171         BOOL got_policy_hnd = False;
172         DOM_SID *sids;
173         char **names;
174         uint32 *types;
175         int num_names, i;
176         TALLOC_CTX *mem_ctx;
177
178         if (argc == 1) {
179                 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
180                 return NT_STATUS_OK;
181         }
182
183         if (!(mem_ctx = talloc_init())) {
184                 DEBUG(0,("cmd_lsa_lookup_sids: talloc_init failed\n"));
185                 return NT_STATUS_UNSUCCESSFUL;
186         }
187
188         /* Initialise RPC connection */
189         if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
190                 DEBUG(0, ("Could not initialize samr pipe!\n"));
191                 talloc_destroy(mem_ctx);
192                 return NT_STATUS_UNSUCCESSFUL;
193         }
194
195         result = cli_lsa_open_policy(cli, mem_ctx, True, 
196                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
197                                      &pol);
198         if (!NT_STATUS_IS_OK(result)) {
199                 goto done;
200         }
201
202         got_policy_hnd = True;
203
204         /* Convert arguments to sids */
205
206         sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * (argc - 1));
207
208         if (!sids) {
209                 printf("out of memory\n");
210                 goto done;
211         }
212
213         for (i = 0; i < argc - 1; i++) {
214                 string_to_sid(&sids[i], argv[i + 1]);
215         }
216
217         /* Lookup the SIDs */
218
219         result = cli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids, 
220                                      &names, &types, &num_names);
221         if (!NT_STATUS_IS_OK(result)) {
222                 goto done;
223         }
224
225         /* Print results */
226
227         for (i = 0; i < num_names; i++) {
228                 fstring sid_str;
229
230                 sid_to_string(sid_str, &sids[i]);
231                 printf("%s %s (%d)\n", sid_str, names[i] ? names[i] :
232                        "*unknown*", types[i]);
233         }
234
235 #if 0   /* JERRY */
236         SAFE_FREE(sids);
237         SAFE_FREE(types);      
238
239         for (i = 0; i < num_names; i++) {
240                 SAFE_FREE(names[i]);
241         }
242
243         SAFE_FREE(names);
244 #endif
245
246  done:
247
248         if (got_policy_hnd) {
249                 cli_lsa_close(cli, mem_ctx, &pol);
250         }
251
252         cli_nt_session_close(cli);
253         talloc_destroy (mem_ctx);
254
255         return result;
256 }
257
258 /* Enumerate list of trusted domains */
259
260 static NTSTATUS cmd_lsa_enum_trust_dom(struct cli_state *cli, int argc, char **argv)
261 {
262         POLICY_HND pol;
263         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
264         BOOL got_policy_hnd = False;
265         DOM_SID *domain_sids;
266         char **domain_names;
267         uint32 enum_ctx = 0;
268         uint32 num_domains;
269         int i;
270         TALLOC_CTX *mem_ctx;
271
272         if (argc != 1) {
273                 printf("Usage: %s\n", argv[0]);
274                 return NT_STATUS_OK;
275         }
276
277         if (!(mem_ctx = talloc_init())) {
278                 DEBUG(0,("cmd_lsa_enum_trust_dom: talloc_init failed\n"));
279                 return NT_STATUS_UNSUCCESSFUL;
280         }
281
282         /* Initialise RPC connection */
283         if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
284                 DEBUG(0, ("Could not initialize samr pipe!\n"));
285                 talloc_destroy(mem_ctx);
286                 return NT_STATUS_UNSUCCESSFUL;
287         }
288
289         result = cli_lsa_open_policy(cli, mem_ctx, True, 
290                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
291                                      &pol);
292         if (!NT_STATUS_IS_OK(result)) {
293                 goto done;
294         }
295
296         got_policy_hnd = True;
297
298         /* Lookup list of trusted domains */
299
300         result = cli_lsa_enum_trust_dom(cli, mem_ctx, &pol, &enum_ctx,
301                                         &num_domains, &domain_names,
302                                         &domain_sids);
303         if (!NT_STATUS_IS_OK(result)) {
304                 goto done;
305         }
306
307         /* Print results */
308
309         for (i = 0; i < num_domains; i++) {
310                 fstring sid_str;
311
312                 sid_to_string(sid_str, &domain_sids[i]);
313                 printf("%s %s\n", domain_names[i] ? domain_names[i] : 
314                        "*unknown*", sid_str);
315         }
316
317  done:
318
319         if (got_policy_hnd) {
320                 cli_lsa_close(cli, mem_ctx, &pol);
321         }
322
323         cli_nt_session_close(cli);
324         talloc_destroy(mem_ctx);
325
326         return result;
327 }
328
329 /* List of commands exported by this module */
330
331 struct cmd_set lsarpc_commands[] = {
332
333         { "LSARPC" },
334
335         { "lsaquery",    cmd_lsa_query_info_policy,     "Query info policy",         "" },
336         { "lookupsids",  cmd_lsa_lookup_sids,           "Convert SIDs to names",     "" },
337         { "lookupnames", cmd_lsa_lookup_names,          "Convert names to SIDs",     "" },
338         { "enumtrust",   cmd_lsa_enum_trust_dom,        "Enumerate trusted domains", "" },
339
340         { NULL }
341 };