lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | Shared library add-on to iptables to add layer 7 matching support. |
| 3 | |
| 4 | By Matthew Strait <quadong@users.sf.net>, Oct 2003. |
| 5 | |
| 6 | http://l7-filter.sf.net |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or |
| 9 | modify it under the terms of the GNU General Public License |
| 10 | as published by the Free Software Foundation; either version |
| 11 | 2 of the License, or (at your option) any later version. |
| 12 | http://www.gnu.org/licenses/gpl.txt |
| 13 | |
| 14 | Based on libipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be> |
| 15 | */ |
| 16 | |
| 17 | #define _GNU_SOURCE |
| 18 | #include <stdio.h> |
| 19 | #include <netdb.h> |
| 20 | #include <string.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <getopt.h> |
| 23 | #include <ctype.h> |
| 24 | #include <dirent.h> |
| 25 | |
| 26 | #include <xtables.h> |
| 27 | #include <linux/netfilter/xt_layer7.h> |
| 28 | |
| 29 | #define MAX_FN_LEN 256 |
| 30 | |
| 31 | static char l7dir[MAX_FN_LEN] = "\0"; |
| 32 | |
| 33 | /* Function which prints out usage message. */ |
| 34 | static void help(void) |
| 35 | { |
| 36 | printf( |
| 37 | "LAYER7 match options:\n" |
| 38 | "--l7dir <directory> : Look for patterns here instead of /etc/l7-protocols/\n" |
| 39 | " (--l7dir must be specified before --l7proto if used!)\n" |
| 40 | "--l7proto [!] <name> : Match the protocol defined in /etc/l7-protocols/name.pat\n"); |
| 41 | fputc('\n', stdout); |
| 42 | } |
| 43 | |
| 44 | static struct option opts[] = { |
| 45 | { .name = "l7proto", .has_arg = 1, .flag = 0, .val = '1' }, |
| 46 | { .name = "l7dir", .has_arg = 1, .flag = 0, .val = '2' }, |
| 47 | { .name = NULL } |
| 48 | }; |
| 49 | |
| 50 | /* reads filename, puts protocol info into layer7_protocol_info, number of protocols to numprotos */ |
| 51 | static int parse_protocol_file(char * filename, const char * protoname, struct xt_layer7_info *info) |
| 52 | { |
| 53 | FILE * f; |
| 54 | char * line = NULL; |
| 55 | size_t len = 0; |
| 56 | |
| 57 | enum { protocol, pattern, done } datatype = protocol; |
| 58 | |
| 59 | f = fopen(filename, "r"); |
| 60 | |
| 61 | if(!f) |
| 62 | return 0; |
| 63 | |
| 64 | while(getline(&line, &len, f) != -1) |
| 65 | { |
| 66 | if(strlen(line) < 2 || line[0] == '#') |
| 67 | continue; |
| 68 | |
| 69 | /* strip the pesky newline... */ |
| 70 | if(line[strlen(line) - 1] == '\n') |
| 71 | line[strlen(line) - 1] = '\0'; |
| 72 | |
| 73 | if(datatype == protocol) |
| 74 | { |
| 75 | /* Ignore everything on the line beginning with the |
| 76 | first space or tab . For instance, this allows the |
| 77 | protocol line in http.pat to be "http " (or |
| 78 | "http I am so cool") instead of just "http". */ |
| 79 | if(strchr(line, ' ')){ |
| 80 | char * space = strchr(line, ' '); |
| 81 | space[0] = '\0'; |
| 82 | } |
| 83 | if(strchr(line, '\t')){ |
| 84 | char * space = strchr(line, '\t'); |
| 85 | space[0] = '\0'; |
| 86 | } |
| 87 | |
| 88 | /* sanity check. First non-comment non-blank |
| 89 | line must be the same as the file name. */ |
| 90 | if(strcmp(line, protoname)) |
| 91 | xtables_error(OTHER_PROBLEM, |
| 92 | "Protocol name (%s) doesn't match file name (%s). Bailing out\n", |
| 93 | line, filename); |
| 94 | |
| 95 | if(strlen(line) >= MAX_PROTOCOL_LEN) |
| 96 | xtables_error(PARAMETER_PROBLEM, |
| 97 | "Protocol name in %s too long!", filename); |
| 98 | strncpy(info->protocol, line, MAX_PROTOCOL_LEN); |
| 99 | |
| 100 | datatype = pattern; |
| 101 | } |
| 102 | else if(datatype == pattern) |
| 103 | { |
| 104 | if(strlen(line) >= MAX_PATTERN_LEN) |
| 105 | xtables_error(PARAMETER_PROBLEM, "Pattern in %s too long!", filename); |
| 106 | strncpy(info->pattern, line, MAX_PATTERN_LEN); |
| 107 | |
| 108 | datatype = done; |
| 109 | break; |
| 110 | } |
| 111 | else |
| 112 | xtables_error(OTHER_PROBLEM, "Internal error"); |
| 113 | } |
| 114 | |
| 115 | if(datatype != done) |
| 116 | xtables_error(OTHER_PROBLEM, "Failed to get all needed data from %s", filename); |
| 117 | |
| 118 | if(line) free(line); |
| 119 | fclose(f); |
| 120 | |
| 121 | return 1; |
| 122 | |
| 123 | /* |
| 124 | fprintf(stderr, "protocol: %s\npattern: %s\n\n", |
| 125 | info->protocol, |
| 126 | info->pattern); |
| 127 | */ |
| 128 | } |
| 129 | |
| 130 | static int hex2dec(char c) |
| 131 | { |
| 132 | switch (c) |
| 133 | { |
| 134 | case '0' ... '9': |
| 135 | return c - '0'; |
| 136 | case 'a' ... 'f': |
| 137 | return c - 'a' + 10; |
| 138 | case 'A' ... 'F': |
| 139 | return c - 'A' + 10; |
| 140 | default: |
| 141 | xtables_error(OTHER_PROBLEM, "hex2dec: bad value!\n"); |
| 142 | return 0; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* takes a string with \xHH escapes and returns one with the characters |
| 147 | they stand for */ |
| 148 | static char * pre_process(char * s) |
| 149 | { |
| 150 | char * result = malloc(strlen(s) + 1); |
| 151 | int sindex = 0, rindex = 0; |
| 152 | while( sindex < strlen(s) ) |
| 153 | { |
| 154 | if( sindex + 3 < strlen(s) && |
| 155 | s[sindex] == '\\' && s[sindex+1] == 'x' && |
| 156 | isxdigit(s[sindex + 2]) && isxdigit(s[sindex + 3]) ) |
| 157 | { |
| 158 | /* carefully remember to call tolower here... */ |
| 159 | result[rindex] = tolower( hex2dec(s[sindex + 2])*16 + |
| 160 | hex2dec(s[sindex + 3] ) ); |
| 161 | |
| 162 | switch ( result[rindex] ) |
| 163 | { |
| 164 | case 0x24: |
| 165 | case 0x28: |
| 166 | case 0x29: |
| 167 | case 0x2a: |
| 168 | case 0x2b: |
| 169 | case 0x2e: |
| 170 | case 0x3f: |
| 171 | case 0x5b: |
| 172 | case 0x5c: |
| 173 | case 0x5e: |
| 174 | case 0x7c: |
| 175 | fprintf(stderr, |
| 176 | "Warning: layer7 regexp contains a control character, %c, in hex (\\x%c%c).\n" |
| 177 | "I recommend that you write this as %c or \\%c, depending on what you meant.\n", |
| 178 | result[rindex], s[sindex + 2], s[sindex + 3], result[rindex], result[rindex]); |
| 179 | break; |
| 180 | case 0x00: |
| 181 | fprintf(stderr, |
| 182 | "Warning: null (\\x00) in layer7 regexp. A null terminates the regexp string!\n"); |
| 183 | break; |
| 184 | default: |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | sindex += 3; /* 4 total */ |
| 190 | } |
| 191 | else |
| 192 | result[rindex] = tolower(s[sindex]); |
| 193 | |
| 194 | sindex++; |
| 195 | rindex++; |
| 196 | } |
| 197 | result[rindex] = '\0'; |
| 198 | |
| 199 | return result; |
| 200 | } |
| 201 | |
| 202 | #define MAX_SUBDIRS 128 |
| 203 | static char ** readl7dir(char * dirname) |
| 204 | { |
| 205 | DIR * scratchdir; |
| 206 | struct dirent ** namelist; |
| 207 | char ** subdirs = malloc(MAX_SUBDIRS * sizeof(char *)); |
| 208 | |
| 209 | int n, d = 1; |
| 210 | subdirs[0] = ""; |
| 211 | |
| 212 | n = scandir(dirname, &namelist, 0, alphasort); |
| 213 | |
| 214 | if (n < 0) |
| 215 | { |
| 216 | perror("scandir"); |
| 217 | xtables_error(OTHER_PROBLEM, "Couldn't open %s\n", dirname); |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | while(n--) |
| 222 | { |
| 223 | char fulldirname[MAX_FN_LEN]; |
| 224 | |
| 225 | snprintf(fulldirname, MAX_FN_LEN, "%s/%s", dirname, namelist[n]->d_name); |
| 226 | |
| 227 | if((scratchdir = opendir(fulldirname)) != NULL) |
| 228 | { |
| 229 | closedir(scratchdir); |
| 230 | |
| 231 | if(!strcmp(namelist[n]->d_name, ".") || |
| 232 | !strcmp(namelist[n]->d_name, "..")) |
| 233 | /* do nothing */ ; |
| 234 | else |
| 235 | { |
| 236 | subdirs[d] = malloc(strlen(namelist[n]->d_name) + 1); |
| 237 | strcpy(subdirs[d], namelist[n]->d_name); |
| 238 | d++; |
| 239 | if(d >= MAX_SUBDIRS - 1) |
| 240 | { |
| 241 | fprintf(stderr, |
| 242 | "Too many subdirectories, skipping the rest!\n"); |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | free(namelist[n]); |
| 248 | } |
| 249 | free(namelist); |
| 250 | } |
| 251 | |
| 252 | subdirs[d] = NULL; |
| 253 | |
| 254 | return subdirs; |
| 255 | } |
| 256 | |
| 257 | static void |
| 258 | parse_layer7_protocol(const char *s, struct xt_layer7_info *info) |
| 259 | { |
| 260 | char filename[MAX_FN_LEN]; |
| 261 | char * dir = NULL; |
| 262 | char ** subdirs; |
| 263 | int n = 0, done = 0; |
| 264 | |
| 265 | if(strlen(l7dir) > 0) |
| 266 | dir = l7dir; |
| 267 | else |
| 268 | dir = "/etc/l7-protocols"; |
| 269 | |
| 270 | subdirs = readl7dir(dir); |
| 271 | |
| 272 | while(subdirs[n] != NULL) |
| 273 | { |
| 274 | int c = snprintf(filename, MAX_FN_LEN, "%s/%s/%s.pat", dir, subdirs[n], s); |
| 275 | |
| 276 | //fprintf(stderr, "Trying to find pattern in %s ... ", filename); |
| 277 | |
| 278 | if(c > MAX_FN_LEN) |
| 279 | { |
| 280 | xtables_error(OTHER_PROBLEM, |
| 281 | "Filename beginning with %s is too long!\n", filename); |
| 282 | } |
| 283 | |
| 284 | /* read in the pattern from the file */ |
| 285 | if(parse_protocol_file(filename, s, info)) |
| 286 | { |
| 287 | //fprintf(stderr, "found\n"); |
| 288 | done = 1; |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | //fprintf(stderr, "not found\n"); |
| 293 | |
| 294 | n++; |
| 295 | } |
| 296 | |
| 297 | if(!done) |
| 298 | xtables_error(OTHER_PROBLEM, |
| 299 | "Couldn't find a pattern definition file for %s.\n", s); |
| 300 | |
| 301 | /* process \xHH escapes and tolower everything. (our regex lib has no |
| 302 | case insensitivity option.) */ |
| 303 | strncpy(info->pattern, pre_process(info->pattern), MAX_PATTERN_LEN); |
| 304 | } |
| 305 | |
| 306 | /* Function which parses command options; returns true if it ate an option */ |
| 307 | static int parse(int c, char **argv, int invert, unsigned int *flags, |
| 308 | const void *entry, |
| 309 | struct xt_entry_match **match) |
| 310 | { |
| 311 | struct xt_layer7_info *layer7info = |
| 312 | (struct xt_layer7_info *)(*match)->data; |
| 313 | |
| 314 | switch (c) { |
| 315 | case '1': |
| 316 | xtables_check_inverse(optarg, &invert, &optind, 0); |
| 317 | parse_layer7_protocol(argv[optind-1], layer7info); |
| 318 | if (invert) |
| 319 | layer7info->invert = 1; |
| 320 | *flags = 1; |
| 321 | break; |
| 322 | |
| 323 | case '2': |
| 324 | /* not going to use this, but maybe we need to strip a ! anyway (?) */ |
| 325 | xtables_check_inverse(optarg, &invert, &optind, 0); |
| 326 | |
| 327 | if(strlen(argv[optind-1]) >= MAX_FN_LEN) |
| 328 | xtables_error(PARAMETER_PROBLEM, "directory name too long\n"); |
| 329 | |
| 330 | strncpy(l7dir, argv[optind-1], MAX_FN_LEN); |
| 331 | |
| 332 | *flags = 1; |
| 333 | break; |
| 334 | |
| 335 | default: |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | return 1; |
| 340 | } |
| 341 | |
| 342 | /* Final check; must have specified --l7proto */ |
| 343 | static void final_check(unsigned int flags) |
| 344 | { |
| 345 | if (!flags) |
| 346 | xtables_error(PARAMETER_PROBLEM, |
| 347 | "LAYER7 match: You must specify `--l7proto'"); |
| 348 | } |
| 349 | |
| 350 | static void print_protocol(char s[], int invert, int numeric) |
| 351 | { |
| 352 | fputs("l7proto ", stdout); |
| 353 | if (invert) fputc('!', stdout); |
| 354 | printf("%s ", s); |
| 355 | } |
| 356 | |
| 357 | /* Prints out the matchinfo. */ |
| 358 | static void print(const void *ip, |
| 359 | const struct xt_entry_match *match, |
| 360 | int numeric) |
| 361 | { |
| 362 | printf("LAYER7 "); |
| 363 | |
| 364 | print_protocol(((struct xt_layer7_info *)match->data)->protocol, |
| 365 | ((struct xt_layer7_info *)match->data)->invert, numeric); |
| 366 | } |
| 367 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
| 368 | static void save(const void *ip, const struct xt_entry_match *match) |
| 369 | { |
| 370 | const struct xt_layer7_info *info = |
| 371 | (const struct xt_layer7_info*) match->data; |
| 372 | |
| 373 | printf("--l7proto %s%s ", (info->invert) ? "! ": "", info->protocol); |
| 374 | } |
| 375 | |
| 376 | static struct xtables_match layer7_match = { |
| 377 | .family = NFPROTO_IPV4, |
| 378 | .name = "layer7", |
| 379 | .version = XTABLES_VERSION, |
| 380 | .size = XT_ALIGN(sizeof(struct xt_layer7_info)), |
| 381 | .userspacesize = XT_ALIGN(sizeof(struct xt_layer7_info)), |
| 382 | .help = &help, |
| 383 | .parse = &parse, |
| 384 | .final_check = &final_check, |
| 385 | .print = &print, |
| 386 | .save = &save, |
| 387 | .extra_opts = opts |
| 388 | }; |
| 389 | |
| 390 | static struct xtables_match layer7_match6 = { |
| 391 | .family = NFPROTO_IPV6, |
| 392 | .name = "layer7", |
| 393 | .version = XTABLES_VERSION, |
| 394 | .size = XT_ALIGN(sizeof(struct xt_layer7_info)), |
| 395 | .userspacesize = XT_ALIGN(sizeof(struct xt_layer7_info)), |
| 396 | .help = &help, |
| 397 | .parse = &parse, |
| 398 | .final_check = &final_check, |
| 399 | .print = &print, |
| 400 | .save = &save, |
| 401 | .extra_opts = opts |
| 402 | }; |
| 403 | |
| 404 | void _init(void) |
| 405 | { |
| 406 | xtables_register_match(&layer7_match); |
| 407 | xtables_register_match(&layer7_match6); |
| 408 | } |