s3: Fix uninitialized memory read in talloc_free()
[Samba.git] / source3 / utils / net_rpc_audit.c
blobceea9f69704381cc640511eaeafa15b1b4c758eb
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( "%s\n"
368 "net rpc audit get\n"
369 " %s\n",
370 _("Usage:"),
371 _("View configured audit setting"));
372 return 0;
375 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
376 rpc_audit_get_internal, argc, argv);
379 /********************************************************************
380 ********************************************************************/
382 static int rpc_audit_set(struct net_context *c, int argc, const char **argv)
384 if (c->display_usage) {
385 d_printf( "%s\n"
386 "net rpc audit set\n"
387 " %s\n",
388 _("Usage:"),
389 _("Set audit policies"));
390 return 0;
393 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
394 rpc_audit_set_internal, argc, argv);
397 /********************************************************************
398 ********************************************************************/
400 static int rpc_audit_enable(struct net_context *c, int argc, const char **argv)
402 if (c->display_usage) {
403 d_printf( "%s\n"
404 "net rpc audit enable\n"
405 " %s\n",
406 _("Usage:"),
407 _("Enable auditing"));
408 return 0;
411 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
412 rpc_audit_enable_internal, argc, argv);
415 /********************************************************************
416 ********************************************************************/
418 static int rpc_audit_disable(struct net_context *c, int argc, const char **argv)
420 if (c->display_usage) {
421 d_printf( "%s\n"
422 "net rpc audit disable\n"
423 " %s\n",
424 _("Usage:"),
425 _("Disable auditing"));
426 return 0;
429 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
430 rpc_audit_disable_internal, argc, argv);
433 /********************************************************************
434 ********************************************************************/
436 static int rpc_audit_list(struct net_context *c, int argc, const char **argv)
438 if (c->display_usage) {
439 d_printf( "%s\n"
440 "net rpc audit list\n"
441 " %s\n",
442 _("Usage:"),
443 _("List auditing settings"));
444 return 0;
447 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
448 rpc_audit_list_internal, argc, argv);
451 /********************************************************************
452 ********************************************************************/
454 int net_rpc_audit(struct net_context *c, int argc, const char **argv)
456 struct functable func[] = {
458 "get",
459 rpc_audit_get,
460 NET_TRANSPORT_RPC,
461 N_("View configured auditing settings"),
462 N_("net rpc audit get\n"
463 " View configured auditing settings")
466 "set",
467 rpc_audit_set,
468 NET_TRANSPORT_RPC,
469 N_("Set auditing policies"),
470 N_("net rpc audit set\n"
471 " Set auditing policies")
474 "enable",
475 rpc_audit_enable,
476 NET_TRANSPORT_RPC,
477 N_("Enable auditing"),
478 N_("net rpc audit enable\n"
479 " Enable auditing")
482 "disable",
483 rpc_audit_disable,
484 NET_TRANSPORT_RPC,
485 N_("Disable auditing"),
486 N_("net rpc audit disable\n"
487 " Disable auditing")
490 "list",
491 rpc_audit_list,
492 NET_TRANSPORT_RPC,
493 N_("List configured auditing settings"),
494 N_("net rpc audit list\n"
495 " List configured auditing settings")
497 {NULL, NULL, 0, NULL, NULL}
500 return net_run_function(c, argc, argv, "net rpc audit", func);