libipt_realm.c revision 967279231a9ecfa99f26694a954afc535c63db1d
1/* Shared library add-on to iptables to add realm matching support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <errno.h>
7#include <ctype.h>
8#include <getopt.h>
9#if defined(__GLIBC__) && __GLIBC__ == 2
10#include <net/ethernet.h>
11#else
12#include <linux/if_ether.h>
13#endif
14#include <iptables.h>
15#include <linux/netfilter_ipv4/ipt_realm.h>
16
17/* Function which prints out usage message. */
18static void realm_help(void)
19{
20	printf(
21"realm match options:\n"
22"[!] --realm value[/mask]\n"
23"				Match realm\n");
24}
25
26static const struct option realm_opts[] = {
27	{ "realm", 1, NULL, '1' },
28	{ .name = NULL }
29};
30
31struct realmname {
32	int	id;
33	char*	name;
34	int	len;
35	struct realmname* next;
36};
37
38/* array of realms from /etc/iproute2/rt_realms */
39static struct realmname *realms = NULL;
40/* 1 if loading failed */
41static int rdberr = 0;
42
43
44static void load_realms(void)
45{
46	const char* rfnm = "/etc/iproute2/rt_realms";
47	char buf[512];
48	FILE *fil;
49	char *cur, *nxt;
50	int id;
51	struct realmname *oldnm = NULL, *newnm = NULL;
52
53	fil = fopen(rfnm, "r");
54	if (!fil) {
55		rdberr = 1;
56		return;
57	}
58
59	while (fgets(buf, sizeof(buf), fil)) {
60		cur = buf;
61		while ((*cur == ' ') || (*cur == '\t'))
62			cur++;
63		if ((*cur == '#') || (*cur == '\n') || (*cur == 0))
64			continue;
65
66		/* iproute2 allows hex and dec format */
67		errno = 0;
68		id = strtoul(cur, &nxt, strncmp(cur, "0x", 2) ? 10 : 16);
69		if ((nxt == cur) || errno)
70			continue;
71
72		/* same boundaries as in iproute2 */
73		if (id < 0 || id > 255)
74			continue;
75		cur = nxt;
76
77		if (!isspace(*cur))
78			continue;
79		while ((*cur == ' ') || (*cur == '\t'))
80			cur++;
81		if ((*cur == '#') || (*cur == '\n') || (*cur == 0))
82			continue;
83		nxt = cur;
84		while ((*nxt != 0) && !isspace(*nxt))
85			nxt++;
86		if (nxt == cur)
87			continue;
88
89		/* found valid data */
90		newnm = (struct realmname*)malloc(sizeof(struct realmname));
91		if (newnm == NULL) {
92			perror("libipt_realm: malloc failed");
93			exit(1);
94		}
95		newnm->id = id;
96		newnm->len = nxt - cur;
97		newnm->name = (char*)malloc(newnm->len + 1);
98		if (newnm->name == NULL) {
99			perror("libipt_realm: malloc failed");
100			exit(1);
101		}
102		strncpy(newnm->name, cur, newnm->len);
103		newnm->name[newnm->len] = 0;
104		newnm->next = NULL;
105
106		if (oldnm)
107			oldnm->next = newnm;
108		else
109			realms = newnm;
110		oldnm = newnm;
111	}
112
113	fclose(fil);
114}
115
116/* get realm id for name, -1 if error/not found */
117static int realm_name2id(const char* name)
118{
119	struct realmname* cur;
120
121	if ((realms == NULL) && (rdberr == 0))
122		load_realms();
123	cur = realms;
124	if (cur == NULL)
125		return -1;
126	while (cur) {
127		if (!strncmp(name, cur->name, cur->len + 1))
128			return cur->id;
129		cur = cur->next;
130	}
131	return -1;
132}
133
134/* get realm name for id, NULL if error/not found */
135static const char *realm_id2name(int id)
136{
137	struct realmname* cur;
138
139	if ((realms == NULL) && (rdberr == 0))
140		load_realms();
141	cur = realms;
142	if (cur == NULL)
143		return NULL;
144	while (cur) {
145		if (id == cur->id)
146			return cur->name;
147		cur = cur->next;
148	}
149	return NULL;
150}
151
152
153/* Function which parses command options; returns true if it
154   ate an option */
155static int realm_parse(int c, char **argv, int invert, unsigned int *flags,
156                       const void *entry, struct xt_entry_match **match)
157{
158	struct ipt_realm_info *realminfo = (struct ipt_realm_info *)(*match)->data;
159	int id;
160
161	switch (c) {
162		char *end;
163	case '1':
164		check_inverse(argv[optind-1], &invert, &optind, 0);
165		end = optarg = argv[optind-1];
166		realminfo->id = strtoul(optarg, &end, 0);
167		if (end != optarg && (*end == '/' || *end == '\0')) {
168			if (*end == '/')
169				realminfo->mask = strtoul(end+1, &end, 0);
170			else
171				realminfo->mask = 0xffffffff;
172			if (*end != '\0' || end == optarg)
173				exit_error(PARAMETER_PROBLEM,
174					   "Bad realm value `%s'", optarg);
175		} else {
176			id = realm_name2id(optarg);
177			if (id == -1)
178				exit_error(PARAMETER_PROBLEM,
179					   "Realm `%s' not found", optarg);
180			realminfo->id = (u_int32_t)id;
181			realminfo->mask = 0xffffffff;
182		}
183		if (invert)
184			realminfo->invert = 1;
185		*flags = 1;
186		break;
187
188	default:
189		return 0;
190	}
191	return 1;
192}
193
194static void
195print_realm(unsigned long id, unsigned long mask, int numeric)
196{
197	const char* name = NULL;
198
199	if (mask != 0xffffffff)
200		printf("0x%lx/0x%lx ", id, mask);
201	else {
202		if (numeric == 0)
203			name = realm_id2name(id);
204		if (name)
205			printf("%s ", name);
206		else
207			printf("0x%lx ", id);
208	}
209}
210
211/* Prints out the matchinfo. */
212static void realm_print(const void *ip, const struct xt_entry_match *match,
213                        int numeric)
214{
215	struct ipt_realm_info *ri = (struct ipt_realm_info *) match->data;
216
217	if (ri->invert)
218		printf("! ");
219
220	printf("realm ");
221	print_realm(ri->id, ri->mask, numeric);
222}
223
224
225/* Saves the union ipt_matchinfo in parsable form to stdout. */
226static void realm_save(const void *ip, const struct xt_entry_match *match)
227{
228	struct ipt_realm_info *ri = (struct ipt_realm_info *) match->data;
229
230	if (ri->invert)
231		printf("! ");
232
233	printf("--realm ");
234	print_realm(ri->id, ri->mask, 0);
235}
236
237/* Final check; must have specified --mark. */
238static void realm_check(unsigned int flags)
239{
240	if (!flags)
241		exit_error(PARAMETER_PROBLEM,
242			   "realm match: You must specify `--realm'");
243}
244
245static struct xtables_match realm_mt_reg = {
246	.name		= "realm",
247	.version	= XTABLES_VERSION,
248	.family		= PF_INET,
249	.size		= XT_ALIGN(sizeof(struct ipt_realm_info)),
250	.userspacesize	= XT_ALIGN(sizeof(struct ipt_realm_info)),
251	.help		= realm_help,
252	.parse		= realm_parse,
253	.final_check	= realm_check,
254	.print		= realm_print,
255	.save		= realm_save,
256	.extra_opts	= realm_opts,
257};
258
259void _init(void)
260{
261	xtables_register_match(&realm_mt_reg);
262}
263
264
265