Harald Welte | 2e2d3f3 | 2001-09-02 15:34:18 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add realm matching support. */ |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 6 | #include <errno.h> |
| 7 | #include <ctype.h> |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 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 |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 14 | #include <xtables.h> |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 15 | #include <linux/netfilter_ipv4/ipt_realm.h> |
| 16 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 17 | static void realm_help(void) |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 18 | { |
| 19 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 20 | "realm match options:\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 21 | "[!] --realm value[/mask]\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 22 | " Match realm\n"); |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 25 | static const struct option realm_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 26 | { "realm", 1, NULL, '1' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 27 | { .name = NULL } |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 30 | struct 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 */ |
| 38 | static struct realmname *realms = NULL; |
| 39 | /* 1 if loading failed */ |
| 40 | static int rdberr = 0; |
| 41 | |
| 42 | |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 43 | static void load_realms(void) |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 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 = (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 Engelhardt | 0e2abed | 2007-10-04 16:25:58 +0000 | [diff] [blame] | 116 | static int realm_name2id(const char* name) |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 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 */ |
Jan Engelhardt | 0e2abed | 2007-10-04 16:25:58 +0000 | [diff] [blame] | 134 | static const char *realm_id2name(int id) |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 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 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 151 | static int realm_parse(int c, char **argv, int invert, unsigned int *flags, |
| 152 | const void *entry, struct xt_entry_match **match) |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 153 | { |
| 154 | struct ipt_realm_info *realminfo = (struct ipt_realm_info *)(*match)->data; |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 155 | int id; |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 156 | |
| 157 | switch (c) { |
| 158 | char *end; |
| 159 | case '1': |
Harald Welte | 69558bf | 2005-02-13 18:17:11 +0000 | [diff] [blame] | 160 | check_inverse(argv[optind-1], &invert, &optind, 0); |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 161 | end = optarg = argv[optind-1]; |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 162 | realminfo->id = strtoul(optarg, &end, 0); |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 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 | 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 Engelhardt | 213e185 | 2009-01-27 17:24:34 +0100 | [diff] [blame^] | 176 | realminfo->id = id; |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 177 | realminfo->mask = 0xffffffff; |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 178 | } |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 179 | if (invert) |
| 180 | realminfo->invert = 1; |
| 181 | *flags = 1; |
| 182 | break; |
| 183 | |
| 184 | default: |
| 185 | return 0; |
| 186 | } |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | static void |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 191 | print_realm(unsigned long id, unsigned long mask, int numeric) |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 192 | { |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 193 | const char* name = NULL; |
| 194 | |
Harald Welte | 61d274f | 2005-02-08 15:54:17 +0000 | [diff] [blame] | 195 | if (mask != 0xffffffff) |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 196 | printf("0x%lx/0x%lx ", id, mask); |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 197 | 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 Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 207 | static void realm_print(const void *ip, const struct xt_entry_match *match, |
| 208 | int numeric) |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 209 | { |
Harald Welte | 61d274f | 2005-02-08 15:54:17 +0000 | [diff] [blame] | 210 | struct ipt_realm_info *ri = (struct ipt_realm_info *) match->data; |
| 211 | |
| 212 | if (ri->invert) |
| 213 | printf("! "); |
| 214 | |
Simon Lodal | a895b9c | 2006-05-24 16:25:09 +0000 | [diff] [blame] | 215 | printf("realm "); |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 216 | print_realm(ri->id, ri->mask, numeric); |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 219 | static void realm_save(const void *ip, const struct xt_entry_match *match) |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 220 | { |
Harald Welte | 61d274f | 2005-02-08 15:54:17 +0000 | [diff] [blame] | 221 | struct ipt_realm_info *ri = (struct ipt_realm_info *) match->data; |
| 222 | |
| 223 | if (ri->invert) |
| 224 | printf("! "); |
| 225 | |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 226 | printf("--realm "); |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 227 | print_realm(ri->id, ri->mask, 0); |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 230 | static void realm_check(unsigned int flags) |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 231 | { |
| 232 | if (!flags) |
| 233 | exit_error(PARAMETER_PROBLEM, |
Simon Lodal | a895b9c | 2006-05-24 16:25:09 +0000 | [diff] [blame] | 234 | "realm match: You must specify `--realm'"); |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 237 | static struct xtables_match realm_mt_reg = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 238 | .name = "realm", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 239 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 240 | .family = NFPROTO_IPV4, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 241 | .size = XT_ALIGN(sizeof(struct ipt_realm_info)), |
| 242 | .userspacesize = XT_ALIGN(sizeof(struct ipt_realm_info)), |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 243 | .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 Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | void _init(void) |
| 252 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 253 | xtables_register_match(&realm_mt_reg); |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 254 | } |