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