libxt_AUDIT.c revision 773438bd93851dc1a9129a638925c04868820297
1/* Shared library add-on to xtables for AUDIT
2 *
3 * (C) 2010-2011, Thomas Graf <tgraf@redhat.com>
4 * (C) 2010-2011, Red Hat, Inc.
5 *
6 * This program is distributed under the terms of GNU GPL v2, 1991
7 */
8
9#include <stdbool.h>
10#include <stdio.h>
11#include <string.h>
12#include <stdlib.h>
13#include <getopt.h>
14
15#include <xtables.h>
16#include <linux/netfilter/xt_AUDIT.h>
17
18static void audit_help(void)
19{
20	printf(
21"AUDIT target options\n"
22"  --type TYPE		Action type to be recorded.\n");
23}
24
25static const struct option audit_opts[] = {
26	{.name = "type", .has_arg = true, .val = 't'},
27	XT_GETOPT_TABLEEND,
28};
29
30static int audit_parse(int c, char **argv, int invert, unsigned int *flags,
31                     const void *entry, struct xt_entry_target **target)
32{
33	struct xt_audit_info *einfo
34		= (struct xt_audit_info *)(*target)->data;
35
36	switch (c) {
37	case 't':
38		if (!strcasecmp(optarg, "accept"))
39			einfo->type = XT_AUDIT_TYPE_ACCEPT;
40		else if (!strcasecmp(optarg, "drop"))
41			einfo->type = XT_AUDIT_TYPE_DROP;
42		else if (!strcasecmp(optarg, "reject"))
43			einfo->type = XT_AUDIT_TYPE_REJECT;
44		else
45			xtables_error(PARAMETER_PROBLEM,
46				   "Bad action type value `%s'", optarg);
47
48		if (*flags)
49			xtables_error(PARAMETER_PROBLEM,
50			           "AUDIT: Can't specify --type twice");
51		*flags = 1;
52		break;
53	default:
54		return 0;
55	}
56
57	return 1;
58}
59
60static void audit_final_check(unsigned int flags)
61{
62	if (!flags)
63		xtables_error(PARAMETER_PROBLEM,
64		           "AUDIT target: Parameter --type is required");
65}
66
67static void audit_print(const void *ip, const struct xt_entry_target *target,
68                      int numeric)
69{
70	const struct xt_audit_info *einfo =
71		(const struct xt_audit_info *)target->data;
72
73	printf("AUDIT ");
74
75	switch(einfo->type) {
76	case XT_AUDIT_TYPE_ACCEPT:
77		printf("accept");
78		break;
79	case XT_AUDIT_TYPE_DROP:
80		printf("drop");
81		break;
82	case XT_AUDIT_TYPE_REJECT:
83		printf("reject");
84		break;
85	}
86}
87
88static void audit_save(const void *ip, const struct xt_entry_target *target)
89{
90	const struct xt_audit_info *einfo =
91		(const struct xt_audit_info *)target->data;
92
93	switch(einfo->type) {
94	case XT_AUDIT_TYPE_ACCEPT:
95		printf("--type=accept");
96		break;
97	case XT_AUDIT_TYPE_DROP:
98		printf("--type=drop");
99		break;
100	case XT_AUDIT_TYPE_REJECT:
101		printf("--type=reject");
102		break;
103	}
104}
105
106static struct xtables_target audit_tg_reg = {
107	.name		= "AUDIT",
108	.version	= XTABLES_VERSION,
109	.family		= NFPROTO_UNSPEC,
110	.size		= XT_ALIGN(sizeof(struct xt_audit_info)),
111	.userspacesize	= XT_ALIGN(sizeof(struct xt_audit_info)),
112	.help		= audit_help,
113	.parse		= audit_parse,
114	.final_check	= audit_final_check,
115	.print		= audit_print,
116	.save		= audit_save,
117	.extra_opts	= audit_opts,
118};
119
120void _init(void)
121{
122	xtables_register_target(&audit_tg_reg);
123}
124