libipt_realm.c revision d09b6d591ca7d7d7575cb6aa20384c9830f777ab
19258b6bc66e09368ada54001f619d53b4fc976d5ager@chromium.org/* Shared library add-on to iptables to add realm matching support. */
29a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include <stdbool.h>
39a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include <stdio.h>
49a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include <netdb.h>
59a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include <string.h>
69a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include <stdlib.h>
79a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include <errno.h>
89a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include <ctype.h>
99a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include <getopt.h>
109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#if defined(__GLIBC__) && __GLIBC__ == 2
119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include <net/ethernet.h>
129a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#else
139a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include <linux/if_ether.h>
149a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#endif
159a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include <xtables.h>
169a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include <linux/netfilter_ipv4/ipt_realm.h>
179a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
189a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comstatic void realm_help(void)
199a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com{
209a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com	printf(
219a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com"realm match options:\n"
229a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com"[!] --realm value[/mask]\n"
239a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com"				Match realm\n");
249a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
259a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
269a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comstatic const struct option realm_opts[] = {
279a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com	{.name = "realm", .has_arg = true, .val = '1'},
289a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com	XT_GETOPT_TABLEEND,
299a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com};
309a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
319a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comstruct realmname {
329a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com	int	id;
339a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com	char*	name;
349a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com	int	len;
359a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com	struct realmname* next;
369a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com};
379a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
38/* array of realms from /etc/iproute2/rt_realms */
39static struct realmname *realms;
40/* 1 if loading failed */
41static int rdberr;
42
43static void load_realms(void)
44{
45	const char* rfnm = "/etc/iproute2/rt_realms";
46	char buf[512];
47	FILE *fil;
48	char *cur, *nxt;
49	int id;
50	struct realmname *oldnm = NULL, *newnm = NULL;
51
52	fil = fopen(rfnm, "r");
53	if (!fil) {
54		rdberr = 1;
55		return;
56	}
57
58	while (fgets(buf, sizeof(buf), fil)) {
59		cur = buf;
60		while ((*cur == ' ') || (*cur == '\t'))
61			cur++;
62		if ((*cur == '#') || (*cur == '\n') || (*cur == 0))
63			continue;
64
65		/* iproute2 allows hex and dec format */
66		errno = 0;
67		id = strtoul(cur, &nxt, strncmp(cur, "0x", 2) ? 10 : 16);
68		if ((nxt == cur) || errno)
69			continue;
70
71		/* same boundaries as in iproute2 */
72		if (id < 0 || id > 255)
73			continue;
74		cur = nxt;
75
76		if (!isspace(*cur))
77			continue;
78		while ((*cur == ' ') || (*cur == '\t'))
79			cur++;
80		if ((*cur == '#') || (*cur == '\n') || (*cur == 0))
81			continue;
82		nxt = cur;
83		while ((*nxt != 0) && !isspace(*nxt))
84			nxt++;
85		if (nxt == cur)
86			continue;
87
88		/* found valid data */
89		newnm = malloc(sizeof(struct realmname));
90		if (newnm == NULL) {
91			perror("libipt_realm: malloc failed");
92			exit(1);
93		}
94		newnm->id = id;
95		newnm->len = nxt - cur;
96		newnm->name = malloc(newnm->len + 1);
97		if (newnm->name == NULL) {
98			perror("libipt_realm: malloc failed");
99			exit(1);
100		}
101		strncpy(newnm->name, cur, newnm->len);
102		newnm->name[newnm->len] = 0;
103		newnm->next = NULL;
104
105		if (oldnm)
106			oldnm->next = newnm;
107		else
108			realms = newnm;
109		oldnm = newnm;
110	}
111
112	fclose(fil);
113}
114
115/* get realm id for name, -1 if error/not found */
116static int realm_name2id(const char* name)
117{
118	struct realmname* cur;
119
120	if ((realms == NULL) && (rdberr == 0))
121		load_realms();
122	cur = realms;
123	if (cur == NULL)
124		return -1;
125	while (cur) {
126		if (!strncmp(name, cur->name, cur->len + 1))
127			return cur->id;
128		cur = cur->next;
129	}
130	return -1;
131}
132
133/* get realm name for id, NULL if error/not found */
134static const char *realm_id2name(int id)
135{
136	struct realmname* cur;
137
138	if ((realms == NULL) && (rdberr == 0))
139		load_realms();
140	cur = realms;
141	if (cur == NULL)
142		return NULL;
143	while (cur) {
144		if (id == cur->id)
145			return cur->name;
146		cur = cur->next;
147	}
148	return NULL;
149}
150
151static int realm_parse(int c, char **argv, int invert, unsigned int *flags,
152                       const void *entry, struct xt_entry_match **match)
153{
154	struct ipt_realm_info *realminfo = (struct ipt_realm_info *)(*match)->data;
155	int id;
156
157	switch (c) {
158		char *end;
159	case '1':
160		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
161		end = optarg = optarg;
162		realminfo->id = strtoul(optarg, &end, 0);
163		if (end != optarg && (*end == '/' || *end == '\0')) {
164			if (*end == '/')
165				realminfo->mask = strtoul(end+1, &end, 0);
166			else
167				realminfo->mask = 0xffffffff;
168			if (*end != '\0' || end == optarg)
169				xtables_error(PARAMETER_PROBLEM,
170					   "Bad realm value `%s'", optarg);
171		} else {
172			id = realm_name2id(optarg);
173			if (id == -1)
174				xtables_error(PARAMETER_PROBLEM,
175					   "Realm `%s' not found", optarg);
176			realminfo->id = id;
177			realminfo->mask = 0xffffffff;
178		}
179		if (invert)
180			realminfo->invert = 1;
181		*flags = 1;
182		break;
183	}
184	return 1;
185}
186
187static void
188print_realm(unsigned long id, unsigned long mask, int numeric)
189{
190	const char* name = NULL;
191
192	if (mask != 0xffffffff)
193		printf("0x%lx/0x%lx ", id, mask);
194	else {
195		if (numeric == 0)
196			name = realm_id2name(id);
197		if (name)
198			printf("%s ", name);
199		else
200			printf("0x%lx ", id);
201	}
202}
203
204static void realm_print(const void *ip, const struct xt_entry_match *match,
205                        int numeric)
206{
207	const struct ipt_realm_info *ri = (const void *)match->data;
208
209	if (ri->invert)
210		printf("! ");
211
212	printf("realm ");
213	print_realm(ri->id, ri->mask, numeric);
214}
215
216static void realm_save(const void *ip, const struct xt_entry_match *match)
217{
218	const struct ipt_realm_info *ri = (const void *)match->data;
219
220	if (ri->invert)
221		printf("! ");
222
223	printf("--realm ");
224	print_realm(ri->id, ri->mask, 0);
225}
226
227static void realm_check(unsigned int flags)
228{
229	if (!flags)
230		xtables_error(PARAMETER_PROBLEM,
231			   "realm match: You must specify `--realm'");
232}
233
234static struct xtables_match realm_mt_reg = {
235	.name		= "realm",
236	.version	= XTABLES_VERSION,
237	.family		= NFPROTO_IPV4,
238	.size		= XT_ALIGN(sizeof(struct ipt_realm_info)),
239	.userspacesize	= XT_ALIGN(sizeof(struct ipt_realm_info)),
240	.help		= realm_help,
241	.parse		= realm_parse,
242	.final_check	= realm_check,
243	.print		= realm_print,
244	.save		= realm_save,
245	.extra_opts	= realm_opts,
246};
247
248void _init(void)
249{
250	xtables_register_match(&realm_mt_reg);
251}
252