Use CommandError exception to deal with problems during net commands.
[Samba/eduardoll.git] / source3 / utils / net_rpc_audit.c
blobb001c9c6236f61f32d99dd5ac5324cae59dc9c06
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2006,2008 Guenther Deschner
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "includes.h"
20 #include "utils/net.h"
21 #include "../librpc/gen_ndr/cli_lsa.h"
23 /********************************************************************
24 ********************************************************************/
26 static int net_help_audit(struct net_context *c, int argc, const char **argv)
28 d_printf(_("net rpc audit list View configured Auditing policies\n"));
29 d_printf(_("net rpc audit enable Enable Auditing\n"));
30 d_printf(_("net rpc audit disable Disable Auditing\n"));
31 d_printf(_("net rpc audit get <category> View configured Auditing policy setting\n"));
32 d_printf(_("net rpc audit set <category> <policy> Set Auditing policies\n\n"));
33 d_printf(_("\tcategory can be one of: SYSTEM, LOGON, OBJECT, PRIVILEGE, PROCESS, POLICY, SAM, DIRECTORY or ACCOUNT\n"));
34 d_printf(_("\tpolicy can be one of: SUCCESS, FAILURE, ALL or NONE\n\n"));
36 return -1;
39 /********************************************************************
40 ********************************************************************/
42 static void print_auditing_category(const char *policy, const char *value)
44 if (policy == NULL) {
45 policy = N_("Unknown");
47 if (value == NULL) {
48 value = N_("Invalid");
51 d_printf(_("\t%-30s%s\n"), policy, value);
54 /********************************************************************
55 ********************************************************************/
57 static NTSTATUS rpc_audit_get_internal(struct net_context *c,
58 const DOM_SID *domain_sid,
59 const char *domain_name,
60 struct cli_state *cli,
61 struct rpc_pipe_client *pipe_hnd,
62 TALLOC_CTX *mem_ctx,
63 int argc,
64 const char **argv)
66 struct policy_handle pol;
67 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
68 union lsa_PolicyInformation *info = NULL;
69 int i;
70 uint32_t audit_category;
72 if (argc < 1 || argc > 2) {
73 d_printf(_("insufficient arguments\n"));
74 net_help_audit(c, argc, argv);
75 return NT_STATUS_INVALID_PARAMETER;
78 if (!get_audit_category_from_param(argv[0], &audit_category)) {
79 d_printf(_("invalid auditing category: %s\n"), argv[0]);
80 return NT_STATUS_INVALID_PARAMETER;
83 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
84 SEC_FLAG_MAXIMUM_ALLOWED,
85 &pol);
87 if (!NT_STATUS_IS_OK(result)) {
88 goto done;
91 result = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
92 &pol,
93 LSA_POLICY_INFO_AUDIT_EVENTS,
94 &info);
96 if (!NT_STATUS_IS_OK(result)) {
97 goto done;
100 for (i=0; i < info->audit_events.count; i++) {
102 const char *val = NULL, *policy = NULL;
104 if (i != audit_category) {
105 continue;
108 val = audit_policy_str(mem_ctx, info->audit_events.settings[i]);
109 policy = audit_description_str(i);
110 print_auditing_category(policy, val);
113 done:
114 if (!NT_STATUS_IS_OK(result)) {
115 d_printf(_("failed to get auditing policy: %s\n"),
116 nt_errstr(result));
119 return result;
122 /********************************************************************
123 ********************************************************************/
125 static NTSTATUS rpc_audit_set_internal(struct net_context *c,
126 const DOM_SID *domain_sid,
127 const char *domain_name,
128 struct cli_state *cli,
129 struct rpc_pipe_client *pipe_hnd,
130 TALLOC_CTX *mem_ctx,
131 int argc,
132 const char **argv)
134 struct policy_handle pol;
135 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
136 union lsa_PolicyInformation *info = NULL;
137 uint32_t audit_policy, audit_category;
139 if (argc < 2 || argc > 3) {
140 d_printf(_("insufficient arguments\n"));
141 net_help_audit(c, argc, argv);
142 return NT_STATUS_INVALID_PARAMETER;
145 if (!get_audit_category_from_param(argv[0], &audit_category)) {
146 d_printf(_("invalid auditing category: %s\n"), argv[0]);
147 return NT_STATUS_INVALID_PARAMETER;
150 audit_policy = LSA_AUDIT_POLICY_CLEAR;
152 if (strequal(argv[1], "Success")) {
153 audit_policy |= LSA_AUDIT_POLICY_SUCCESS;
154 } else if (strequal(argv[1], "Failure")) {
155 audit_policy |= LSA_AUDIT_POLICY_FAILURE;
156 } else if (strequal(argv[1], "All")) {
157 audit_policy |= LSA_AUDIT_POLICY_ALL;
158 } else if (strequal(argv[1], "None")) {
159 audit_policy = LSA_AUDIT_POLICY_CLEAR;
160 } else {
161 d_printf(_("invalid auditing policy: %s\n"), argv[1]);
162 return NT_STATUS_INVALID_PARAMETER;
165 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
166 SEC_FLAG_MAXIMUM_ALLOWED,
167 &pol);
169 if (!NT_STATUS_IS_OK(result)) {
170 goto done;
173 result = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
174 &pol,
175 LSA_POLICY_INFO_AUDIT_EVENTS,
176 &info);
178 if (!NT_STATUS_IS_OK(result)) {
179 goto done;
182 info->audit_events.settings[audit_category] = audit_policy;
184 result = rpccli_lsa_SetInfoPolicy(pipe_hnd, mem_ctx,
185 &pol,
186 LSA_POLICY_INFO_AUDIT_EVENTS,
187 info);
189 if (!NT_STATUS_IS_OK(result)) {
190 goto done;
193 result = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
194 &pol,
195 LSA_POLICY_INFO_AUDIT_EVENTS,
196 &info);
198 const char *val = audit_policy_str(mem_ctx, info->audit_events.settings[audit_category]);
199 const char *policy = audit_description_str(audit_category);
200 print_auditing_category(policy, val);
203 done:
204 if (!NT_STATUS_IS_OK(result)) {
205 d_printf(_("failed to set audit policy: %s\n"),
206 nt_errstr(result));
209 return result;
212 /********************************************************************
213 ********************************************************************/
215 static NTSTATUS rpc_audit_enable_internal_ext(struct rpc_pipe_client *pipe_hnd,
216 TALLOC_CTX *mem_ctx,
217 int argc,
218 const char **argv,
219 bool enable)
221 struct policy_handle pol;
222 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
223 union lsa_PolicyInformation *info = NULL;
225 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
226 SEC_FLAG_MAXIMUM_ALLOWED,
227 &pol);
229 if (!NT_STATUS_IS_OK(result)) {
230 goto done;
233 result = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
234 &pol,
235 LSA_POLICY_INFO_AUDIT_EVENTS,
236 &info);
237 if (!NT_STATUS_IS_OK(result)) {
238 goto done;
241 info->audit_events.auditing_mode = enable;
243 result = rpccli_lsa_SetInfoPolicy(pipe_hnd, mem_ctx,
244 &pol,
245 LSA_POLICY_INFO_AUDIT_EVENTS,
246 info);
248 if (!NT_STATUS_IS_OK(result)) {
249 goto done;
252 done:
253 if (!NT_STATUS_IS_OK(result)) {
254 d_printf(_("%s: %s\n"),
255 enable ? _("failed to enable audit policy"):
256 _("failed to disable audit policy"),
257 nt_errstr(result));
260 return result;
263 /********************************************************************
264 ********************************************************************/
266 static NTSTATUS rpc_audit_disable_internal(struct net_context *c,
267 const DOM_SID *domain_sid,
268 const char *domain_name,
269 struct cli_state *cli,
270 struct rpc_pipe_client *pipe_hnd,
271 TALLOC_CTX *mem_ctx,
272 int argc,
273 const char **argv)
275 return rpc_audit_enable_internal_ext(pipe_hnd, mem_ctx, argc, argv,
276 false);
279 /********************************************************************
280 ********************************************************************/
282 static NTSTATUS rpc_audit_enable_internal(struct net_context *c,
283 const DOM_SID *domain_sid,
284 const char *domain_name,
285 struct cli_state *cli,
286 struct rpc_pipe_client *pipe_hnd,
287 TALLOC_CTX *mem_ctx,
288 int argc,
289 const char **argv)
291 return rpc_audit_enable_internal_ext(pipe_hnd, mem_ctx, argc, argv,
292 true);
295 /********************************************************************
296 ********************************************************************/
298 static NTSTATUS rpc_audit_list_internal(struct net_context *c,
299 const DOM_SID *domain_sid,
300 const char *domain_name,
301 struct cli_state *cli,
302 struct rpc_pipe_client *pipe_hnd,
303 TALLOC_CTX *mem_ctx,
304 int argc,
305 const char **argv)
307 struct policy_handle pol;
308 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
309 union lsa_PolicyInformation *info = NULL;
310 int i;
312 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
313 SEC_FLAG_MAXIMUM_ALLOWED,
314 &pol);
316 if (!NT_STATUS_IS_OK(result)) {
317 goto done;
320 result = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
321 &pol,
322 LSA_POLICY_INFO_AUDIT_EVENTS,
323 &info);
324 if (!NT_STATUS_IS_OK(result)) {
325 goto done;
328 printf(_("Auditing:\t\t"));
329 switch (info->audit_events.auditing_mode) {
330 case true:
331 printf(_("Enabled"));
332 break;
333 case false:
334 printf(_("Disabled"));
335 break;
336 default:
337 printf(_("unknown (%d)"),
338 info->audit_events.auditing_mode);
339 break;
341 printf("\n");
343 printf(_("Auditing categories:\t%d\n"), info->audit_events.count);
344 printf(_("Auditing settings:\n"));
346 for (i=0; i < info->audit_events.count; i++) {
347 const char *val = audit_policy_str(mem_ctx, info->audit_events.settings[i]);
348 const char *policy = audit_description_str(i);
349 print_auditing_category(policy, val);
352 done:
353 if (!NT_STATUS_IS_OK(result)) {
354 d_printf(_("failed to list auditing policies: %s\n"),
355 nt_errstr(result));
358 return result;
361 /********************************************************************
362 ********************************************************************/
364 static int rpc_audit_get(struct net_context *c, int argc, const char **argv)
366 if (c->display_usage) {
367 d_printf(_("Usage:\n"
368 "net rpc audit get\n"
369 " View configured audit setting\n"));
370 return 0;
373 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
374 rpc_audit_get_internal, argc, argv);
377 /********************************************************************
378 ********************************************************************/
380 static int rpc_audit_set(struct net_context *c, int argc, const char **argv)
382 if (c->display_usage) {
383 d_printf(_("Usage:\n"
384 "net rpc audit set\n"
385 " Set audit policies\n"));
386 return 0;
389 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
390 rpc_audit_set_internal, argc, argv);
393 /********************************************************************
394 ********************************************************************/
396 static int rpc_audit_enable(struct net_context *c, int argc, const char **argv)
398 if (c->display_usage) {
399 d_printf(_("Usage:\n"
400 "net rpc audit enable\n"
401 " Enable auditing\n"));
402 return 0;
405 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
406 rpc_audit_enable_internal, argc, argv);
409 /********************************************************************
410 ********************************************************************/
412 static int rpc_audit_disable(struct net_context *c, int argc, const char **argv)
414 if (c->display_usage) {
415 d_printf(_("Usage:\n"
416 "net rpc audit disable\n"
417 " Disable auditing\n"));
418 return 0;
421 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
422 rpc_audit_disable_internal, argc, argv);
425 /********************************************************************
426 ********************************************************************/
428 static int rpc_audit_list(struct net_context *c, int argc, const char **argv)
430 if (c->display_usage) {
431 d_printf(_("Usage:\n"
432 "net rpc audit list\n"
433 " List auditing settings\n"));
434 return 0;
437 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
438 rpc_audit_list_internal, argc, argv);
441 /********************************************************************
442 ********************************************************************/
444 int net_rpc_audit(struct net_context *c, int argc, const char **argv)
446 struct functable func[] = {
448 "get",
449 rpc_audit_get,
450 NET_TRANSPORT_RPC,
451 N_("View configured auditing settings"),
452 N_("net rpc audit get\n"
453 " View configured auditing settings")
456 "set",
457 rpc_audit_set,
458 NET_TRANSPORT_RPC,
459 N_("Set auditing policies"),
460 N_("net rpc audit set\n"
461 " Set auditing policies")
464 "enable",
465 rpc_audit_enable,
466 NET_TRANSPORT_RPC,
467 N_("Enable auditing"),
468 N_("net rpc audit enable\n"
469 " Enable auditing")
472 "disable",
473 rpc_audit_disable,
474 NET_TRANSPORT_RPC,
475 N_("Disable auditing"),
476 N_("net rpc audit disable\n"
477 " Disable auditing")
480 "list",
481 rpc_audit_list,
482 NET_TRANSPORT_RPC,
483 N_("List configured auditing settings"),
484 N_("net rpc audit list\n"
485 " List configured auditing settings")
487 {NULL, NULL, 0, NULL, NULL}
490 return net_run_function(c, argc, argv, "net rpc audit", func);