summaryrefslogtreecommitdiffstats
path: root/extensions/libxt_AUDIT.c
blob: a6ab37f91ac5d90788348931f6a26cbc69452ca3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* Shared library add-on to xtables for AUDIT
 *
 * (C) 2010-2011, Thomas Graf <tgraf@redhat.com>
 * (C) 2010-2011, Red Hat, Inc.
 *
 * This program is distributed under the terms of GNU GPL v2, 1991
 */

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>

#include <xtables.h>
#include <linux/netfilter/xt_AUDIT.h>

static void audit_help(void)
{
	printf(
"AUDIT target options\n"
"  --type TYPE		Action type to be recorded.\n");
}

static const struct option audit_opts[] = {
	{.name = "type", .has_arg = true, .val = 't'},
	XT_GETOPT_TABLEEND,
};

static int audit_parse(int c, char **argv, int invert, unsigned int *flags,
                     const void *entry, struct xt_entry_target **target)
{
	struct xt_audit_info *einfo
		= (struct xt_audit_info *)(*target)->data;

	switch (c) {
	case 't':
		if (!strcasecmp(optarg, "accept"))
			einfo->type = XT_AUDIT_TYPE_ACCEPT;
		else if (!strcasecmp(optarg, "drop"))
			einfo->type = XT_AUDIT_TYPE_DROP;
		else if (!strcasecmp(optarg, "reject"))
			einfo->type = XT_AUDIT_TYPE_REJECT;
		else
			xtables_error(PARAMETER_PROBLEM,
				   "Bad action type value `%s'", optarg);

		if (*flags)
			xtables_error(PARAMETER_PROBLEM,
			           "AUDIT: Can't specify --type twice");
		*flags = 1;
		break;
	default:
		return 0;
	}

	return 1;
}

static void audit_final_check(unsigned int flags)
{
	if (!flags)
		xtables_error(PARAMETER_PROBLEM,
		           "AUDIT target: Parameter --type is required");
}

static void audit_print(const void *ip, const struct xt_entry_target *target,
                      int numeric)
{
	const struct xt_audit_info *einfo =
		(const struct xt_audit_info *)target->data;

	printf(" AUDIT ");

	switch(einfo->type) {
	case XT_AUDIT_TYPE_ACCEPT:
		printf("accept");
		break;
	case XT_AUDIT_TYPE_DROP:
		printf("drop");
		break;
	case XT_AUDIT_TYPE_REJECT:
		printf("reject");
		break;
	}
}

static void audit_save(const void *ip, const struct xt_entry_target *target)
{
	const struct xt_audit_info *einfo =
		(const struct xt_audit_info *)target->data;

	switch(einfo->type) {
	case XT_AUDIT_TYPE_ACCEPT:
		printf(" --type accept");
		break;
	case XT_AUDIT_TYPE_DROP:
		printf(" --type drop");
		break;
	case XT_AUDIT_TYPE_REJECT:
		printf(" --type reject");
		break;
	}
}

static struct xtables_target audit_tg_reg = {
	.name		= "AUDIT",
	.version	= XTABLES_VERSION,
	.family		= NFPROTO_UNSPEC,
	.size		= XT_ALIGN(sizeof(struct xt_audit_info)),
	.userspacesize	= XT_ALIGN(sizeof(struct xt_audit_info)),
	.help		= audit_help,
	.parse		= audit_parse,
	.final_check	= audit_final_check,
	.print		= audit_print,
	.save		= audit_save,
	.extra_opts	= audit_opts,
};

void _init(void)
{
	xtables_register_target(&audit_tg_reg);
}