blob: 17b1754e69a99e31b3efd8b1a2d69013f0404ca3 [file] [log] [blame]
Harald Welte2e2d3f32001-09-02 15:34:18 +00001/* Shared library add-on to iptables to add realm matching support. */
Jan Engelhardt32b8e612010-07-23 21:16:14 +02002#include <stdbool.h>
Sampsa Rantad6aa9662001-07-30 13:30:14 +00003#include <stdio.h>
4#include <netdb.h>
5#include <string.h>
6#include <stdlib.h>
Simon Lodalca9d8c22006-09-02 12:37:48 +00007#include <errno.h>
8#include <ctype.h>
Sampsa Rantad6aa9662001-07-30 13:30:14 +00009#include <getopt.h>
10#if defined(__GLIBC__) && __GLIBC__ == 2
11#include <net/ethernet.h>
12#else
13#include <linux/if_ether.h>
14#endif
Jan Engelhardt5d9678a2008-11-20 10:15:35 +010015#include <xtables.h>
Sampsa Rantad6aa9662001-07-30 13:30:14 +000016#include <linux/netfilter_ipv4/ipt_realm.h>
17
Jan Engelhardt59d16402007-10-04 16:28:39 +000018static void realm_help(void)
Sampsa Rantad6aa9662001-07-30 13:30:14 +000019{
20 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020021"realm match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020022"[!] --realm value[/mask]\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020023" Match realm\n");
Sampsa Rantad6aa9662001-07-30 13:30:14 +000024}
25
Jan Engelhardt59d16402007-10-04 16:28:39 +000026static const struct option realm_opts[] = {
Jan Engelhardt32b8e612010-07-23 21:16:14 +020027 {.name = "realm", .has_arg = true, .val = '1'},
28 XT_GETOPT_TABLEEND,
Sampsa Rantad6aa9662001-07-30 13:30:14 +000029};
30
Simon Lodalca9d8c22006-09-02 12:37:48 +000031struct 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 */
Jan Engelhardt4a0fbe32009-10-24 01:30:28 +020039static struct realmname *realms;
Simon Lodalca9d8c22006-09-02 12:37:48 +000040/* 1 if loading failed */
Jan Engelhardt4a0fbe32009-10-24 01:30:28 +020041static int rdberr;
Simon Lodalca9d8c22006-09-02 12:37:48 +000042
Patrick McHardy500f4832007-09-08 15:59:04 +000043static void load_realms(void)
Simon Lodalca9d8c22006-09-02 12:37:48 +000044{
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
Maciej Zenczykowskia2397282011-04-04 15:30:32 +020052 fil = fopen(rfnm, "re");
Simon Lodalca9d8c22006-09-02 12:37:48 +000053 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 */
Jan Engelhardtecd48dd2009-06-08 15:46:52 +020089 newnm = malloc(sizeof(struct realmname));
Simon Lodalca9d8c22006-09-02 12:37:48 +000090 if (newnm == NULL) {
91 perror("libipt_realm: malloc failed");
92 exit(1);
93 }
94 newnm->id = id;
95 newnm->len = nxt - cur;
Jan Engelhardtecd48dd2009-06-08 15:46:52 +020096 newnm->name = malloc(newnm->len + 1);
Simon Lodalca9d8c22006-09-02 12:37:48 +000097 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 */
Jan Engelhardt0e2abed2007-10-04 16:25:58 +0000116static int realm_name2id(const char* name)
Simon Lodalca9d8c22006-09-02 12:37:48 +0000117{
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 */
Jan Engelhardt0e2abed2007-10-04 16:25:58 +0000134static const char *realm_id2name(int id)
Simon Lodalca9d8c22006-09-02 12:37:48 +0000135{
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
Jan Engelhardt59d16402007-10-04 16:28:39 +0000151static int realm_parse(int c, char **argv, int invert, unsigned int *flags,
152 const void *entry, struct xt_entry_match **match)
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000153{
154 struct ipt_realm_info *realminfo = (struct ipt_realm_info *)(*match)->data;
Simon Lodalca9d8c22006-09-02 12:37:48 +0000155 int id;
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000156
157 switch (c) {
158 char *end;
159 case '1':
Jan Engelhardtbbe83862009-10-24 00:45:33 +0200160 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
161 end = optarg = optarg;
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000162 realminfo->id = strtoul(optarg, &end, 0);
Simon Lodalca9d8c22006-09-02 12:37:48 +0000163 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)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100169 xtables_error(PARAMETER_PROBLEM,
Simon Lodalca9d8c22006-09-02 12:37:48 +0000170 "Bad realm value `%s'", optarg);
171 } else {
172 id = realm_name2id(optarg);
173 if (id == -1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100174 xtables_error(PARAMETER_PROBLEM,
Simon Lodalca9d8c22006-09-02 12:37:48 +0000175 "Realm `%s' not found", optarg);
Jan Engelhardt213e1852009-01-27 17:24:34 +0100176 realminfo->id = id;
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000177 realminfo->mask = 0xffffffff;
Simon Lodalca9d8c22006-09-02 12:37:48 +0000178 }
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000179 if (invert)
180 realminfo->invert = 1;
181 *flags = 1;
182 break;
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000183 }
184 return 1;
185}
186
187static void
Simon Lodalca9d8c22006-09-02 12:37:48 +0000188print_realm(unsigned long id, unsigned long mask, int numeric)
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000189{
Simon Lodalca9d8c22006-09-02 12:37:48 +0000190 const char* name = NULL;
191
Harald Welte61d274f2005-02-08 15:54:17 +0000192 if (mask != 0xffffffff)
Jan Engelhardt73866352010-12-18 02:04:59 +0100193 printf(" 0x%lx/0x%lx", id, mask);
Simon Lodalca9d8c22006-09-02 12:37:48 +0000194 else {
195 if (numeric == 0)
196 name = realm_id2name(id);
197 if (name)
Jan Engelhardt73866352010-12-18 02:04:59 +0100198 printf(" %s", name);
Simon Lodalca9d8c22006-09-02 12:37:48 +0000199 else
Jan Engelhardt73866352010-12-18 02:04:59 +0100200 printf(" 0x%lx", id);
Simon Lodalca9d8c22006-09-02 12:37:48 +0000201 }
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000202}
203
Jan Engelhardt59d16402007-10-04 16:28:39 +0000204static void realm_print(const void *ip, const struct xt_entry_match *match,
205 int numeric)
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000206{
Jan Engelhardt69f564e2009-05-26 13:14:06 +0200207 const struct ipt_realm_info *ri = (const void *)match->data;
Harald Welte61d274f2005-02-08 15:54:17 +0000208
209 if (ri->invert)
Jan Engelhardt73866352010-12-18 02:04:59 +0100210 printf(" !");
Harald Welte61d274f2005-02-08 15:54:17 +0000211
Jan Engelhardt73866352010-12-18 02:04:59 +0100212 printf(" realm");
Simon Lodalca9d8c22006-09-02 12:37:48 +0000213 print_realm(ri->id, ri->mask, numeric);
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000214}
215
Jan Engelhardt59d16402007-10-04 16:28:39 +0000216static void realm_save(const void *ip, const struct xt_entry_match *match)
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000217{
Jan Engelhardt69f564e2009-05-26 13:14:06 +0200218 const struct ipt_realm_info *ri = (const void *)match->data;
Harald Welte61d274f2005-02-08 15:54:17 +0000219
220 if (ri->invert)
Jan Engelhardt73866352010-12-18 02:04:59 +0100221 printf(" !");
Harald Welte61d274f2005-02-08 15:54:17 +0000222
Jan Engelhardt73866352010-12-18 02:04:59 +0100223 printf(" --realm");
Simon Lodalca9d8c22006-09-02 12:37:48 +0000224 print_realm(ri->id, ri->mask, 0);
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000225}
226
Jan Engelhardt59d16402007-10-04 16:28:39 +0000227static void realm_check(unsigned int flags)
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000228{
229 if (!flags)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100230 xtables_error(PARAMETER_PROBLEM,
Simon Lodala895b9c2006-05-24 16:25:09 +0000231 "realm match: You must specify `--realm'");
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000232}
233
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200234static struct xtables_match realm_mt_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000235 .name = "realm",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200236 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100237 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200238 .size = XT_ALIGN(sizeof(struct ipt_realm_info)),
239 .userspacesize = XT_ALIGN(sizeof(struct ipt_realm_info)),
Jan Engelhardt59d16402007-10-04 16:28:39 +0000240 .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,
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000246};
247
248void _init(void)
249{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200250 xtables_register_match(&realm_mt_reg);
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000251}