2010-03-26 16:28:40 +02:00
|
|
|
--- /dev/null
|
2010-12-19 13:16:46 +02:00
|
|
|
+++ b/extensions/libxt_layer7.c
|
2008-12-01 22:27:46 +02:00
|
|
|
@@ -0,0 +1,368 @@
|
2005-04-02 00:05:15 +03:00
|
|
|
+/*
|
2008-12-01 22:27:46 +02:00
|
|
|
+ Shared library add-on to iptables for layer 7 matching support.
|
2005-04-02 00:05:15 +03:00
|
|
|
+
|
2008-12-01 22:27:46 +02:00
|
|
|
+ By Matthew Strait <quadong@users.sf.net>, Oct 2003-Aug 2008.
|
2005-04-02 00:05:15 +03:00
|
|
|
+
|
|
|
|
+ http://l7-filter.sf.net
|
|
|
|
+
|
|
|
|
+ This program is free software; you can redistribute it and/or
|
|
|
|
+ modify it under the terms of the GNU General Public License
|
|
|
|
+ as published by the Free Software Foundation; either version
|
|
|
|
+ 2 of the License, or (at your option) any later version.
|
|
|
|
+ http://www.gnu.org/licenses/gpl.txt
|
|
|
|
+*/
|
|
|
|
+
|
|
|
|
+#define _GNU_SOURCE
|
|
|
|
+#include <stdio.h>
|
|
|
|
+#include <netdb.h>
|
|
|
|
+#include <string.h>
|
|
|
|
+#include <stdlib.h>
|
|
|
|
+#include <getopt.h>
|
|
|
|
+#include <ctype.h>
|
|
|
|
+#include <dirent.h>
|
|
|
|
+
|
2008-12-01 22:27:46 +02:00
|
|
|
+#include <xtables.h>
|
2008-04-15 09:11:23 +03:00
|
|
|
+#include <linux/netfilter/xt_layer7.h>
|
2005-04-02 00:05:15 +03:00
|
|
|
+
|
|
|
|
+#define MAX_FN_LEN 256
|
|
|
|
+
|
|
|
|
+static char l7dir[MAX_FN_LEN] = "\0";
|
|
|
|
+
|
|
|
|
+/* Function which prints out usage message. */
|
|
|
|
+static void help(void)
|
|
|
|
+{
|
|
|
|
+ printf(
|
2008-12-01 22:27:46 +02:00
|
|
|
+ "layer7 match options:\n"
|
|
|
|
+ " --l7dir <directory> : Look for patterns here instead of /etc/l7-protocols/\n"
|
|
|
|
+ " (--l7dir must be specified before --l7proto if used)\n"
|
|
|
|
+ "[!] --l7proto <name>: Match named protocol using /etc/l7-protocols/.../name.pat\n");
|
2005-04-02 00:05:15 +03:00
|
|
|
+}
|
|
|
|
+
|
2008-12-01 22:27:46 +02:00
|
|
|
+static const struct option opts[] = {
|
|
|
|
+ { .name = "l7proto", .has_arg = 1, .val = 'p' },
|
|
|
|
+ { .name = "l7dir", .has_arg = 1, .val = 'd' },
|
|
|
|
+ { .name = NULL }
|
2005-04-02 00:05:15 +03:00
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/* reads filename, puts protocol info into layer7_protocol_info, number of protocols to numprotos */
|
2008-12-01 22:27:46 +02:00
|
|
|
+static int parse_protocol_file(char * filename, const char * protoname, struct xt_layer7_info *info)
|
2005-04-02 00:05:15 +03:00
|
|
|
+{
|
|
|
|
+ FILE * f;
|
|
|
|
+ char * line = NULL;
|
|
|
|
+ size_t len = 0;
|
|
|
|
+
|
|
|
|
+ enum { protocol, pattern, done } datatype = protocol;
|
|
|
|
+
|
|
|
|
+ f = fopen(filename, "r");
|
|
|
|
+
|
|
|
|
+ if(!f)
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ while(getline(&line, &len, f) != -1)
|
|
|
|
+ {
|
|
|
|
+ if(strlen(line) < 2 || line[0] == '#')
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ /* strip the pesky newline... */
|
|
|
|
+ if(line[strlen(line) - 1] == '\n')
|
|
|
|
+ line[strlen(line) - 1] = '\0';
|
|
|
|
+
|
|
|
|
+ if(datatype == protocol)
|
|
|
|
+ {
|
2008-04-15 09:11:23 +03:00
|
|
|
+ /* Ignore everything on the line beginning with the
|
|
|
|
+ first space or tab . For instance, this allows the
|
|
|
|
+ protocol line in http.pat to be "http " (or
|
|
|
|
+ "http I am so cool") instead of just "http". */
|
|
|
|
+ if(strchr(line, ' ')){
|
|
|
|
+ char * space = strchr(line, ' ');
|
|
|
|
+ space[0] = '\0';
|
|
|
|
+ }
|
|
|
|
+ if(strchr(line, '\t')){
|
|
|
|
+ char * space = strchr(line, '\t');
|
|
|
|
+ space[0] = '\0';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* sanity check. First non-comment non-blank
|
|
|
|
+ line must be the same as the file name. */
|
2005-04-02 00:05:15 +03:00
|
|
|
+ if(strcmp(line, protoname))
|
2009-05-02 00:30:43 +03:00
|
|
|
+ xtables_error(OTHER_PROBLEM,
|
2005-04-02 00:05:15 +03:00
|
|
|
+ "Protocol name (%s) doesn't match file name (%s). Bailing out\n",
|
2008-04-15 09:11:23 +03:00
|
|
|
+ line, filename);
|
2005-04-02 00:05:15 +03:00
|
|
|
+
|
|
|
|
+ if(strlen(line) >= MAX_PROTOCOL_LEN)
|
2009-05-02 00:30:43 +03:00
|
|
|
+ xtables_error(PARAMETER_PROBLEM,
|
2005-04-02 00:05:15 +03:00
|
|
|
+ "Protocol name in %s too long!", filename);
|
|
|
|
+ strncpy(info->protocol, line, MAX_PROTOCOL_LEN);
|
|
|
|
+
|
|
|
|
+ datatype = pattern;
|
|
|
|
+ }
|
|
|
|
+ else if(datatype == pattern)
|
|
|
|
+ {
|
|
|
|
+ if(strlen(line) >= MAX_PATTERN_LEN)
|
2009-05-02 00:30:43 +03:00
|
|
|
+ xtables_error(PARAMETER_PROBLEM, "Pattern in %s too long!", filename);
|
2005-04-02 00:05:15 +03:00
|
|
|
+ strncpy(info->pattern, line, MAX_PATTERN_LEN);
|
|
|
|
+
|
|
|
|
+ datatype = done;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ else
|
2009-05-02 00:30:43 +03:00
|
|
|
+ xtables_error(OTHER_PROBLEM, "Internal error");
|
2005-04-02 00:05:15 +03:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(datatype != done)
|
2009-05-02 00:30:43 +03:00
|
|
|
+ xtables_error(OTHER_PROBLEM, "Failed to get all needed data from %s", filename);
|
2005-04-02 00:05:15 +03:00
|
|
|
+
|
|
|
|
+ if(line) free(line);
|
|
|
|
+ fclose(f);
|
|
|
|
+
|
|
|
|
+ return 1;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int hex2dec(char c)
|
|
|
|
+{
|
|
|
|
+ switch (c)
|
|
|
|
+ {
|
|
|
|
+ case '0' ... '9':
|
|
|
|
+ return c - '0';
|
|
|
|
+ case 'a' ... 'f':
|
|
|
|
+ return c - 'a' + 10;
|
|
|
|
+ case 'A' ... 'F':
|
|
|
|
+ return c - 'A' + 10;
|
|
|
|
+ default:
|
2009-05-02 00:30:43 +03:00
|
|
|
+ xtables_error(OTHER_PROBLEM, "hex2dec: bad value!\n");
|
2005-04-02 00:05:15 +03:00
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* takes a string with \xHH escapes and returns one with the characters
|
|
|
|
+they stand for */
|
|
|
|
+static char * pre_process(char * s)
|
|
|
|
+{
|
|
|
|
+ char * result = malloc(strlen(s) + 1);
|
2008-12-01 22:27:46 +02:00
|
|
|
+ int sindex = 0, rrindex = 0;
|
2005-04-02 00:05:15 +03:00
|
|
|
+ while( sindex < strlen(s) )
|
|
|
|
+ {
|
|
|
|
+ if( sindex + 3 < strlen(s) &&
|
|
|
|
+ s[sindex] == '\\' && s[sindex+1] == 'x' &&
|
|
|
|
+ isxdigit(s[sindex + 2]) && isxdigit(s[sindex + 3]) )
|
|
|
|
+ {
|
|
|
|
+ /* carefully remember to call tolower here... */
|
2008-12-01 22:27:46 +02:00
|
|
|
+ result[rrindex] = tolower( hex2dec(s[sindex + 2])*16 +
|
2005-04-02 00:05:15 +03:00
|
|
|
+ hex2dec(s[sindex + 3] ) );
|
2008-04-15 09:11:23 +03:00
|
|
|
+
|
2008-12-01 22:27:46 +02:00
|
|
|
+ switch ( result[rrindex] )
|
2008-04-15 09:11:23 +03:00
|
|
|
+ {
|
|
|
|
+ case 0x24:
|
|
|
|
+ case 0x28:
|
|
|
|
+ case 0x29:
|
|
|
|
+ case 0x2a:
|
|
|
|
+ case 0x2b:
|
|
|
|
+ case 0x2e:
|
|
|
|
+ case 0x3f:
|
|
|
|
+ case 0x5b:
|
|
|
|
+ case 0x5c:
|
|
|
|
+ case 0x5d:
|
|
|
|
+ case 0x5e:
|
|
|
|
+ case 0x7c:
|
|
|
|
+ fprintf(stderr,
|
|
|
|
+ "Warning: layer7 regexp contains a control character, %c, in hex (\\x%c%c).\n"
|
|
|
|
+ "I recommend that you write this as %c or \\%c, depending on what you meant.\n",
|
2008-12-01 22:27:46 +02:00
|
|
|
+ result[rrindex], s[sindex + 2], s[sindex + 3], result[rrindex], result[rrindex]);
|
2008-04-15 09:11:23 +03:00
|
|
|
+ break;
|
|
|
|
+ case 0x00:
|
|
|
|
+ fprintf(stderr,
|
|
|
|
+ "Warning: null (\\x00) in layer7 regexp. A null terminates the regexp string!\n");
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
2005-04-02 00:05:15 +03:00
|
|
|
+ sindex += 3; /* 4 total */
|
|
|
|
+ }
|
|
|
|
+ else
|
2008-12-01 22:27:46 +02:00
|
|
|
+ result[rrindex] = tolower(s[sindex]);
|
2005-04-02 00:05:15 +03:00
|
|
|
+
|
|
|
|
+ sindex++;
|
2008-12-01 22:27:46 +02:00
|
|
|
+ rrindex++;
|
2005-04-02 00:05:15 +03:00
|
|
|
+ }
|
2008-12-01 22:27:46 +02:00
|
|
|
+ result[rrindex] = '\0';
|
2005-04-02 00:05:15 +03:00
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#define MAX_SUBDIRS 128
|
2008-12-01 22:27:46 +02:00
|
|
|
+static char ** readl7dir(char * dirname)
|
2005-04-02 00:05:15 +03:00
|
|
|
+{
|
|
|
|
+ DIR * scratchdir;
|
|
|
|
+ struct dirent ** namelist;
|
|
|
|
+ char ** subdirs = malloc(MAX_SUBDIRS * sizeof(char *));
|
|
|
|
+
|
|
|
|
+ int n, d = 1;
|
|
|
|
+ subdirs[0] = "";
|
|
|
|
+
|
|
|
|
+ n = scandir(dirname, &namelist, 0, alphasort);
|
|
|
|
+
|
|
|
|
+ if (n < 0)
|
|
|
|
+ {
|
|
|
|
+ perror("scandir");
|
2009-05-02 00:30:43 +03:00
|
|
|
+ xtables_error(OTHER_PROBLEM, "Couldn't open %s\n", dirname);
|
2005-04-02 00:05:15 +03:00
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ while(n--)
|
|
|
|
+ {
|
|
|
|
+ char fulldirname[MAX_FN_LEN];
|
|
|
|
+
|
|
|
|
+ snprintf(fulldirname, MAX_FN_LEN, "%s/%s", dirname, namelist[n]->d_name);
|
|
|
|
+
|
|
|
|
+ if((scratchdir = opendir(fulldirname)) != NULL)
|
|
|
|
+ {
|
|
|
|
+ closedir(scratchdir);
|
|
|
|
+
|
|
|
|
+ if(!strcmp(namelist[n]->d_name, ".") ||
|
|
|
|
+ !strcmp(namelist[n]->d_name, ".."))
|
|
|
|
+ /* do nothing */ ;
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ subdirs[d] = malloc(strlen(namelist[n]->d_name) + 1);
|
|
|
|
+ strcpy(subdirs[d], namelist[n]->d_name);
|
|
|
|
+ d++;
|
|
|
|
+ if(d >= MAX_SUBDIRS - 1)
|
|
|
|
+ {
|
|
|
|
+ fprintf(stderr,
|
|
|
|
+ "Too many subdirectories, skipping the rest!\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ free(namelist[n]);
|
|
|
|
+ }
|
|
|
|
+ free(namelist);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ subdirs[d] = NULL;
|
|
|
|
+
|
|
|
|
+ return subdirs;
|
|
|
|
+}
|
|
|
|
+
|
2008-12-01 22:27:46 +02:00
|
|
|
+static void parse_layer7_protocol(const char *s, struct xt_layer7_info *info)
|
2005-04-02 00:05:15 +03:00
|
|
|
+{
|
|
|
|
+ char filename[MAX_FN_LEN];
|
|
|
|
+ char * dir = NULL;
|
|
|
|
+ char ** subdirs;
|
|
|
|
+ int n = 0, done = 0;
|
|
|
|
+
|
2008-12-01 22:27:46 +02:00
|
|
|
+ if(strlen(l7dir) > 0) dir = l7dir;
|
|
|
|
+ else dir = "/etc/l7-protocols";
|
2005-04-02 00:05:15 +03:00
|
|
|
+
|
|
|
|
+ subdirs = readl7dir(dir);
|
|
|
|
+
|
|
|
|
+ while(subdirs[n] != NULL)
|
|
|
|
+ {
|
|
|
|
+ int c = snprintf(filename, MAX_FN_LEN, "%s/%s/%s.pat", dir, subdirs[n], s);
|
|
|
|
+
|
|
|
|
+ if(c > MAX_FN_LEN)
|
2009-05-02 00:30:43 +03:00
|
|
|
+ xtables_error(OTHER_PROBLEM,
|
2005-04-02 00:05:15 +03:00
|
|
|
+ "Filename beginning with %s is too long!\n", filename);
|
|
|
|
+
|
|
|
|
+ /* read in the pattern from the file */
|
2008-12-01 22:27:46 +02:00
|
|
|
+ if(parse_protocol_file(filename, s, info)){
|
2005-04-02 00:05:15 +03:00
|
|
|
+ done = 1;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ n++;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(!done)
|
2009-05-02 00:30:43 +03:00
|
|
|
+ xtables_error(OTHER_PROBLEM,
|
2005-04-02 00:05:15 +03:00
|
|
|
+ "Couldn't find a pattern definition file for %s.\n", s);
|
|
|
|
+
|
|
|
|
+ /* process \xHH escapes and tolower everything. (our regex lib has no
|
|
|
|
+ case insensitivity option.) */
|
|
|
|
+ strncpy(info->pattern, pre_process(info->pattern), MAX_PATTERN_LEN);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Function which parses command options; returns true if it ate an option */
|
|
|
|
+static int parse(int c, char **argv, int invert, unsigned int *flags,
|
2008-04-15 09:11:23 +03:00
|
|
|
+ const void *entry, struct xt_entry_match **match)
|
2005-04-02 00:05:15 +03:00
|
|
|
+{
|
2008-04-15 09:11:23 +03:00
|
|
|
+ struct xt_layer7_info *layer7info =
|
|
|
|
+ (struct xt_layer7_info *)(*match)->data;
|
2005-04-02 00:05:15 +03:00
|
|
|
+
|
|
|
|
+ switch (c) {
|
2008-12-01 22:27:46 +02:00
|
|
|
+ case 'p':
|
2005-04-02 00:05:15 +03:00
|
|
|
+ parse_layer7_protocol(argv[optind-1], layer7info);
|
|
|
|
+ if (invert)
|
2008-12-01 22:27:46 +02:00
|
|
|
+ layer7info->invert = true;
|
2005-04-02 00:05:15 +03:00
|
|
|
+ *flags = 1;
|
|
|
|
+ break;
|
|
|
|
+
|
2008-12-01 22:27:46 +02:00
|
|
|
+ case 'd':
|
2005-04-02 00:05:15 +03:00
|
|
|
+ if(strlen(argv[optind-1]) >= MAX_FN_LEN)
|
2009-05-02 00:30:43 +03:00
|
|
|
+ xtables_error(PARAMETER_PROBLEM, "directory name too long\n");
|
2005-04-02 00:05:15 +03:00
|
|
|
+
|
|
|
|
+ strncpy(l7dir, argv[optind-1], MAX_FN_LEN);
|
|
|
|
+
|
|
|
|
+ *flags = 1;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 1;
|
|
|
|
+}
|
|
|
|
+
|
2008-04-15 09:11:23 +03:00
|
|
|
+/* Final check; must have specified --l7proto */
|
2005-04-02 00:05:15 +03:00
|
|
|
+static void final_check(unsigned int flags)
|
|
|
|
+{
|
|
|
|
+ if (!flags)
|
2009-05-02 00:30:43 +03:00
|
|
|
+ xtables_error(PARAMETER_PROBLEM,
|
2008-04-15 09:11:23 +03:00
|
|
|
+ "LAYER7 match: You must specify `--l7proto'");
|
2005-04-02 00:05:15 +03:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void print_protocol(char s[], int invert, int numeric)
|
|
|
|
+{
|
|
|
|
+ fputs("l7proto ", stdout);
|
|
|
|
+ if (invert) fputc('!', stdout);
|
|
|
|
+ printf("%s ", s);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Prints out the matchinfo. */
|
2008-04-15 09:11:23 +03:00
|
|
|
+static void print(const void *ip,
|
|
|
|
+ const struct xt_entry_match *match,
|
2005-04-02 00:05:15 +03:00
|
|
|
+ int numeric)
|
|
|
|
+{
|
|
|
|
+ printf("LAYER7 ");
|
2008-04-15 09:11:23 +03:00
|
|
|
+ print_protocol(((struct xt_layer7_info *)match->data)->protocol,
|
|
|
|
+ ((struct xt_layer7_info *)match->data)->invert, numeric);
|
2005-04-02 00:05:15 +03:00
|
|
|
+}
|
|
|
|
+/* Saves the union ipt_matchinfo in parsable form to stdout. */
|
2008-04-15 09:11:23 +03:00
|
|
|
+static void save(const void *ip, const struct xt_entry_match *match)
|
2005-04-02 00:05:15 +03:00
|
|
|
+{
|
2008-04-15 09:11:23 +03:00
|
|
|
+ const struct xt_layer7_info *info =
|
|
|
|
+ (const struct xt_layer7_info*) match->data;
|
2005-04-02 00:05:15 +03:00
|
|
|
+
|
2008-12-01 22:27:46 +02:00
|
|
|
+ printf("--l7proto %s%s ", (info->invert)? "! ":"", info->protocol);
|
2005-04-02 00:05:15 +03:00
|
|
|
+}
|
|
|
|
+
|
2008-12-01 22:27:46 +02:00
|
|
|
+static struct xtables_match layer7 = {
|
|
|
|
+ .family = AF_INET,
|
2005-04-02 00:05:15 +03:00
|
|
|
+ .name = "layer7",
|
2008-12-01 22:27:46 +02:00
|
|
|
+ .version = XTABLES_VERSION,
|
|
|
|
+ .size = XT_ALIGN(sizeof(struct xt_layer7_info)),
|
|
|
|
+ .userspacesize = XT_ALIGN(sizeof(struct xt_layer7_info)),
|
2005-04-02 00:05:15 +03:00
|
|
|
+ .help = &help,
|
|
|
|
+ .parse = &parse,
|
|
|
|
+ .final_check = &final_check,
|
|
|
|
+ .print = &print,
|
|
|
|
+ .save = &save,
|
|
|
|
+ .extra_opts = opts
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+void _init(void)
|
|
|
|
+{
|
2008-12-01 22:27:46 +02:00
|
|
|
+ xtables_register_match(&layer7);
|
2005-04-02 00:05:15 +03:00
|
|
|
+}
|