blob: 22cbe276cac814f69800f5f10404a58959dc1d00 [file] [log] [blame]
Harald Welte2e2d3f32001-09-02 15:34:18 +00001/* Shared library add-on to iptables to add realm matching support. */
Sampsa Rantad6aa9662001-07-30 13:30:14 +00002#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
Simon Lodalca9d8c22006-09-02 12:37:48 +00006#include <errno.h>
7#include <ctype.h>
Sampsa Rantad6aa9662001-07-30 13:30:14 +00008#include <getopt.h>
9#if defined(__GLIBC__) && __GLIBC__ == 2
10#include <net/ethernet.h>
11#else
12#include <linux/if_ether.h>
13#endif
Jan Engelhardt5d9678a2008-11-20 10:15:35 +010014#include <xtables.h>
Sampsa Rantad6aa9662001-07-30 13:30:14 +000015#include <linux/netfilter_ipv4/ipt_realm.h>
16
Jan Engelhardt59d16402007-10-04 16:28:39 +000017static void realm_help(void)
Sampsa Rantad6aa9662001-07-30 13:30:14 +000018{
19 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020020"realm match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020021"[!] --realm value[/mask]\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020022" Match realm\n");
Sampsa Rantad6aa9662001-07-30 13:30:14 +000023}
24
Jan Engelhardt59d16402007-10-04 16:28:39 +000025static const struct option realm_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000026 { "realm", 1, NULL, '1' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000027 { .name = NULL }
Sampsa Rantad6aa9662001-07-30 13:30:14 +000028};
29
Simon Lodalca9d8c22006-09-02 12:37:48 +000030struct realmname {
31 int id;
32 char* name;
33 int len;
34 struct realmname* next;
35};
36
37/* array of realms from /etc/iproute2/rt_realms */
38static struct realmname *realms = NULL;
39/* 1 if loading failed */
40static int rdberr = 0;
41
42
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
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 = (struct realmname*)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 = (char*)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 */
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':
Harald Welte69558bf2005-02-13 18:17:11 +0000160 check_inverse(argv[optind-1], &invert, &optind, 0);
Simon Lodalca9d8c22006-09-02 12:37:48 +0000161 end = optarg = argv[optind-1];
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)
169 exit_error(PARAMETER_PROBLEM,
170 "Bad realm value `%s'", optarg);
171 } else {
172 id = realm_name2id(optarg);
173 if (id == -1)
174 exit_error(PARAMETER_PROBLEM,
175 "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;
183
184 default:
185 return 0;
186 }
187 return 1;
188}
189
190static void
Simon Lodalca9d8c22006-09-02 12:37:48 +0000191print_realm(unsigned long id, unsigned long mask, int numeric)
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000192{
Simon Lodalca9d8c22006-09-02 12:37:48 +0000193 const char* name = NULL;
194
Harald Welte61d274f2005-02-08 15:54:17 +0000195 if (mask != 0xffffffff)
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000196 printf("0x%lx/0x%lx ", id, mask);
Simon Lodalca9d8c22006-09-02 12:37:48 +0000197 else {
198 if (numeric == 0)
199 name = realm_id2name(id);
200 if (name)
201 printf("%s ", name);
202 else
203 printf("0x%lx ", id);
204 }
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000205}
206
Jan Engelhardt59d16402007-10-04 16:28:39 +0000207static void realm_print(const void *ip, const struct xt_entry_match *match,
208 int numeric)
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000209{
Harald Welte61d274f2005-02-08 15:54:17 +0000210 struct ipt_realm_info *ri = (struct ipt_realm_info *) match->data;
211
212 if (ri->invert)
213 printf("! ");
214
Simon Lodala895b9c2006-05-24 16:25:09 +0000215 printf("realm ");
Simon Lodalca9d8c22006-09-02 12:37:48 +0000216 print_realm(ri->id, ri->mask, numeric);
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000217}
218
Jan Engelhardt59d16402007-10-04 16:28:39 +0000219static void realm_save(const void *ip, const struct xt_entry_match *match)
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000220{
Harald Welte61d274f2005-02-08 15:54:17 +0000221 struct ipt_realm_info *ri = (struct ipt_realm_info *) match->data;
222
223 if (ri->invert)
224 printf("! ");
225
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000226 printf("--realm ");
Simon Lodalca9d8c22006-09-02 12:37:48 +0000227 print_realm(ri->id, ri->mask, 0);
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000228}
229
Jan Engelhardt59d16402007-10-04 16:28:39 +0000230static void realm_check(unsigned int flags)
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000231{
232 if (!flags)
233 exit_error(PARAMETER_PROBLEM,
Simon Lodala895b9c2006-05-24 16:25:09 +0000234 "realm match: You must specify `--realm'");
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000235}
236
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200237static struct xtables_match realm_mt_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000238 .name = "realm",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200239 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100240 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200241 .size = XT_ALIGN(sizeof(struct ipt_realm_info)),
242 .userspacesize = XT_ALIGN(sizeof(struct ipt_realm_info)),
Jan Engelhardt59d16402007-10-04 16:28:39 +0000243 .help = realm_help,
244 .parse = realm_parse,
245 .final_check = realm_check,
246 .print = realm_print,
247 .save = realm_save,
248 .extra_opts = realm_opts,
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000249};
250
251void _init(void)
252{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200253 xtables_register_match(&realm_mt_reg);
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000254}