~ubuntu-branches/ubuntu/lucid/iptables/lucid

5.1.4 by Soren Hansen
Import upstream version 1.4.0
1
/* Shared library add-on to iptables to add LOG support. */
2
#include <stdio.h>
3
#include <netdb.h>
4
#include <string.h>
5
#include <stdlib.h>
6
#include <syslog.h>
7
#include <getopt.h>
8
#include <iptables.h>
9
#include <linux/netfilter_ipv4/ip_tables.h>
10
#include <linux/netfilter_ipv4/ipt_LOG.h>
11
12
#define LOG_DEFAULT_LEVEL LOG_WARNING
13
14
#ifndef IPT_LOG_UID /* Old kernel */
15
#define IPT_LOG_UID	0x08	/* Log UID owning local socket */
16
#undef  IPT_LOG_MASK
17
#define IPT_LOG_MASK	0x0f
18
#endif
19
20
/* Function which prints out usage message. */
21
static void LOG_help(void)
22
{
23
	printf(
5.1.5 by Soren Hansen
Import upstream version 1.4.1.1
24
"LOG target options:\n"
5.1.4 by Soren Hansen
Import upstream version 1.4.0
25
" --log-level level		Level of logging (numeric or see syslog.conf)\n"
26
" --log-prefix prefix		Prefix log messages with this prefix.\n\n"
27
" --log-tcp-sequence		Log TCP sequence numbers.\n\n"
28
" --log-tcp-options		Log TCP options.\n\n"
29
" --log-ip-options		Log IP options.\n\n"
5.1.5 by Soren Hansen
Import upstream version 1.4.1.1
30
" --log-uid			Log UID owning the local socket.\n\n");
5.1.4 by Soren Hansen
Import upstream version 1.4.0
31
}
32
33
static const struct option LOG_opts[] = {
34
	{ .name = "log-level",        .has_arg = 1, .val = '!' },
35
	{ .name = "log-prefix",       .has_arg = 1, .val = '#' },
36
	{ .name = "log-tcp-sequence", .has_arg = 0, .val = '1' },
37
	{ .name = "log-tcp-options",  .has_arg = 0, .val = '2' },
38
	{ .name = "log-ip-options",   .has_arg = 0, .val = '3' },
39
	{ .name = "log-uid",          .has_arg = 0, .val = '4' },
5.1.5 by Soren Hansen
Import upstream version 1.4.1.1
40
	{ .name = NULL }
5.1.4 by Soren Hansen
Import upstream version 1.4.0
41
};
42
43
/* Initialize the target. */
44
static void LOG_init(struct xt_entry_target *t)
45
{
46
	struct ipt_log_info *loginfo = (struct ipt_log_info *)t->data;
47
48
	loginfo->level = LOG_DEFAULT_LEVEL;
49
50
}
51
52
struct ipt_log_names {
53
	const char *name;
54
	unsigned int level;
55
};
56
57
static const struct ipt_log_names ipt_log_names[]
58
= { { .name = "alert",   .level = LOG_ALERT },
59
    { .name = "crit",    .level = LOG_CRIT },
60
    { .name = "debug",   .level = LOG_DEBUG },
61
    { .name = "emerg",   .level = LOG_EMERG },
62
    { .name = "error",   .level = LOG_ERR },		/* DEPRECATED */
63
    { .name = "info",    .level = LOG_INFO },
64
    { .name = "notice",  .level = LOG_NOTICE },
65
    { .name = "panic",   .level = LOG_EMERG },		/* DEPRECATED */
66
    { .name = "warning", .level = LOG_WARNING }
67
};
68
69
static u_int8_t
70
parse_level(const char *level)
71
{
72
	unsigned int lev = -1;
73
	unsigned int set = 0;
74
75
	if (string_to_number(level, 0, 7, &lev) == -1) {
76
		unsigned int i = 0;
77
78
		for (i = 0;
79
		     i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
80
		     i++) {
81
			if (strncasecmp(level, ipt_log_names[i].name,
82
					strlen(level)) == 0) {
83
				if (set++)
84
					exit_error(PARAMETER_PROBLEM,
85
						   "log-level `%s' ambiguous",
86
						   level);
87
				lev = ipt_log_names[i].level;
88
			}
89
		}
90
91
		if (!set)
92
			exit_error(PARAMETER_PROBLEM,
93
				   "log-level `%s' unknown", level);
94
	}
95
96
	return (u_int8_t)lev;
97
}
98
99
#define IPT_LOG_OPT_LEVEL 0x01
100
#define IPT_LOG_OPT_PREFIX 0x02
101
#define IPT_LOG_OPT_TCPSEQ 0x04
102
#define IPT_LOG_OPT_TCPOPT 0x08
103
#define IPT_LOG_OPT_IPOPT 0x10
104
#define IPT_LOG_OPT_UID 0x20
105
106
/* Function which parses command options; returns true if it
107
   ate an option */
108
static int LOG_parse(int c, char **argv, int invert, unsigned int *flags,
109
                     const void *entry, struct xt_entry_target **target)
110
{
111
	struct ipt_log_info *loginfo = (struct ipt_log_info *)(*target)->data;
112
113
	switch (c) {
114
	case '!':
115
		if (*flags & IPT_LOG_OPT_LEVEL)
116
			exit_error(PARAMETER_PROBLEM,
117
				   "Can't specify --log-level twice");
118
119
		if (check_inverse(optarg, &invert, NULL, 0))
120
			exit_error(PARAMETER_PROBLEM,
121
				   "Unexpected `!' after --log-level");
122
123
		loginfo->level = parse_level(optarg);
124
		*flags |= IPT_LOG_OPT_LEVEL;
125
		break;
126
127
	case '#':
128
		if (*flags & IPT_LOG_OPT_PREFIX)
129
			exit_error(PARAMETER_PROBLEM,
130
				   "Can't specify --log-prefix twice");
131
132
		if (check_inverse(optarg, &invert, NULL, 0))
133
			exit_error(PARAMETER_PROBLEM,
134
				   "Unexpected `!' after --log-prefix");
135
136
		if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
137
			exit_error(PARAMETER_PROBLEM,
138
				   "Maximum prefix length %u for --log-prefix",
139
				   (unsigned int)sizeof(loginfo->prefix) - 1);
140
141
		if (strlen(optarg) == 0)
142
			exit_error(PARAMETER_PROBLEM,
143
				   "No prefix specified for --log-prefix");
144
145
		if (strlen(optarg) != strlen(strtok(optarg, "\n")))
146
			exit_error(PARAMETER_PROBLEM,
147
				   "Newlines not allowed in --log-prefix");
148
149
		strcpy(loginfo->prefix, optarg);
150
		*flags |= IPT_LOG_OPT_PREFIX;
151
		break;
152
153
	case '1':
154
		if (*flags & IPT_LOG_OPT_TCPSEQ)
155
			exit_error(PARAMETER_PROBLEM,
156
				   "Can't specify --log-tcp-sequence "
157
				   "twice");
158
159
		loginfo->logflags |= IPT_LOG_TCPSEQ;
160
		*flags |= IPT_LOG_OPT_TCPSEQ;
161
		break;
162
163
	case '2':
164
		if (*flags & IPT_LOG_OPT_TCPOPT)
165
			exit_error(PARAMETER_PROBLEM,
166
				   "Can't specify --log-tcp-options twice");
167
168
		loginfo->logflags |= IPT_LOG_TCPOPT;
169
		*flags |= IPT_LOG_OPT_TCPOPT;
170
		break;
171
172
	case '3':
173
		if (*flags & IPT_LOG_OPT_IPOPT)
174
			exit_error(PARAMETER_PROBLEM,
175
				   "Can't specify --log-ip-options twice");
176
177
		loginfo->logflags |= IPT_LOG_IPOPT;
178
		*flags |= IPT_LOG_OPT_IPOPT;
179
		break;
180
181
	case '4':
182
		if (*flags & IPT_LOG_OPT_UID)
183
			exit_error(PARAMETER_PROBLEM,
184
				   "Can't specify --log-uid twice");
185
186
		loginfo->logflags |= IPT_LOG_UID;
187
		*flags |= IPT_LOG_OPT_UID;
188
		break;
189
190
	default:
191
		return 0;
192
	}
193
194
	return 1;
195
}
196
197
/* Prints out the targinfo. */
198
static void LOG_print(const void *ip, const struct xt_entry_target *target,
199
                      int numeric)
200
{
201
	const struct ipt_log_info *loginfo
202
		= (const struct ipt_log_info *)target->data;
203
	unsigned int i = 0;
204
205
	printf("LOG ");
206
	if (numeric)
207
		printf("flags %u level %u ",
208
		       loginfo->logflags, loginfo->level);
209
	else {
210
		for (i = 0;
211
		     i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
212
		     i++) {
213
			if (loginfo->level == ipt_log_names[i].level) {
214
				printf("level %s ", ipt_log_names[i].name);
215
				break;
216
			}
217
		}
218
		if (i == sizeof(ipt_log_names) / sizeof(struct ipt_log_names))
219
			printf("UNKNOWN level %u ", loginfo->level);
220
		if (loginfo->logflags & IPT_LOG_TCPSEQ)
221
			printf("tcp-sequence ");
222
		if (loginfo->logflags & IPT_LOG_TCPOPT)
223
			printf("tcp-options ");
224
		if (loginfo->logflags & IPT_LOG_IPOPT)
225
			printf("ip-options ");
226
		if (loginfo->logflags & IPT_LOG_UID)
227
			printf("uid ");
228
		if (loginfo->logflags & ~(IPT_LOG_MASK))
229
			printf("unknown-flags ");
230
	}
231
232
	if (strcmp(loginfo->prefix, "") != 0)
233
		printf("prefix `%s' ", loginfo->prefix);
234
}
235
236
/* Saves the union ipt_targinfo in parsable form to stdout. */
237
static void LOG_save(const void *ip, const struct xt_entry_target *target)
238
{
239
	const struct ipt_log_info *loginfo
240
		= (const struct ipt_log_info *)target->data;
241
5.1.5 by Soren Hansen
Import upstream version 1.4.1.1
242
	if (strcmp(loginfo->prefix, "") != 0) {
243
		printf("--log-prefix ");
244
		save_string(loginfo->prefix);
245
	}
5.1.4 by Soren Hansen
Import upstream version 1.4.0
246
247
	if (loginfo->level != LOG_DEFAULT_LEVEL)
248
		printf("--log-level %d ", loginfo->level);
249
250
	if (loginfo->logflags & IPT_LOG_TCPSEQ)
251
		printf("--log-tcp-sequence ");
252
	if (loginfo->logflags & IPT_LOG_TCPOPT)
253
		printf("--log-tcp-options ");
254
	if (loginfo->logflags & IPT_LOG_IPOPT)
255
		printf("--log-ip-options ");
256
	if (loginfo->logflags & IPT_LOG_UID)
257
		printf("--log-uid ");
258
}
259
5.1.5 by Soren Hansen
Import upstream version 1.4.1.1
260
static struct xtables_target log_tg_reg = {
5.1.4 by Soren Hansen
Import upstream version 1.4.0
261
    .name          = "LOG",
5.1.5 by Soren Hansen
Import upstream version 1.4.1.1
262
    .version       = XTABLES_VERSION,
263
    .family        = PF_INET,
264
    .size          = XT_ALIGN(sizeof(struct ipt_log_info)),
265
    .userspacesize = XT_ALIGN(sizeof(struct ipt_log_info)),
5.1.4 by Soren Hansen
Import upstream version 1.4.0
266
    .help          = LOG_help,
267
    .init          = LOG_init,
268
    .parse         = LOG_parse,
269
    .print         = LOG_print,
270
    .save          = LOG_save,
271
    .extra_opts    = LOG_opts,
272
};
273
274
void _init(void)
275
{
5.1.5 by Soren Hansen
Import upstream version 1.4.1.1
276
	xtables_register_target(&log_tg_reg);
5.1.4 by Soren Hansen
Import upstream version 1.4.0
277
}