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