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