1 diff -Nur a/libxt_layer7.c b/libxt_layer7.c
2 --- a/libxt_layer7.c 1970-01-01 01:00:00.000000000 +0100
3 +++ b/libxt_layer7.c 2008-08-22 16:00:52.000000000 +0200
6 + Shared library add-on to iptables for layer 7 matching support.
8 + By Matthew Strait <quadong@users.sf.net>, Oct 2003-Aug 2008.
10 + http://l7-filter.sf.net
12 + This program is free software; you can redistribute it and/or
13 + modify it under the terms of the GNU General Public License
14 + as published by the Free Software Foundation; either version
15 + 2 of the License, or (at your option) any later version.
16 + http://www.gnu.org/licenses/gpl.txt
29 +#include <linux/netfilter/xt_layer7.h>
31 +#define MAX_FN_LEN 256
33 +static char l7dir[MAX_FN_LEN] = "\0";
35 +/* Function which prints out usage message. */
36 +static void help(void)
39 + "layer7 match options:\n"
40 + " --l7dir <directory> : Look for patterns here instead of /etc/l7-protocols/\n"
41 + " (--l7dir must be specified before --l7proto if used)\n"
42 + "[!] --l7proto <name>: Match named protocol using /etc/l7-protocols/.../name.pat\n");
45 +static const struct option opts[] = {
46 + { .name = "l7proto", .has_arg = 1, .val = 'p' },
47 + { .name = "l7dir", .has_arg = 1, .val = 'd' },
51 +/* reads filename, puts protocol info into layer7_protocol_info, number of protocols to numprotos */
52 +static int parse_protocol_file(char * filename, const char * protoname, struct xt_layer7_info *info)
58 + enum { protocol, pattern, done } datatype = protocol;
60 + f = fopen(filename, "r");
65 + while(getline(&line, &len, f) != -1)
67 + if(strlen(line) < 2 || line[0] == '#')
70 + /* strip the pesky newline... */
71 + if(line[strlen(line) - 1] == '\n')
72 + line[strlen(line) - 1] = '\0';
74 + if(datatype == protocol)
76 + /* Ignore everything on the line beginning with the
77 + first space or tab . For instance, this allows the
78 + protocol line in http.pat to be "http " (or
79 + "http I am so cool") instead of just "http". */
80 + if(strchr(line, ' ')){
81 + char * space = strchr(line, ' ');
84 + if(strchr(line, '\t')){
85 + char * space = strchr(line, '\t');
89 + /* sanity check. First non-comment non-blank
90 + line must be the same as the file name. */
91 + if(strcmp(line, protoname))
92 + exit_error(OTHER_PROBLEM,
93 + "Protocol name (%s) doesn't match file name (%s). Bailing out\n",
96 + if(strlen(line) >= MAX_PROTOCOL_LEN)
97 + exit_error(PARAMETER_PROBLEM,
98 + "Protocol name in %s too long!", filename);
99 + strncpy(info->protocol, line, MAX_PROTOCOL_LEN);
101 + datatype = pattern;
103 + else if(datatype == pattern)
105 + if(strlen(line) >= MAX_PATTERN_LEN)
106 + exit_error(PARAMETER_PROBLEM, "Pattern in %s too long!", filename);
107 + strncpy(info->pattern, line, MAX_PATTERN_LEN);
113 + exit_error(OTHER_PROBLEM, "Internal error");
116 + if(datatype != done)
117 + exit_error(OTHER_PROBLEM, "Failed to get all needed data from %s", filename);
119 + if(line) free(line);
125 +static int hex2dec(char c)
132 + return c - 'a' + 10;
134 + return c - 'A' + 10;
136 + exit_error(OTHER_PROBLEM, "hex2dec: bad value!\n");
141 +/* takes a string with \xHH escapes and returns one with the characters
143 +static char * pre_process(char * s)
145 + char * result = malloc(strlen(s) + 1);
146 + int sindex = 0, rrindex = 0;
147 + while( sindex < strlen(s) )
149 + if( sindex + 3 < strlen(s) &&
150 + s[sindex] == '\\' && s[sindex+1] == 'x' &&
151 + isxdigit(s[sindex + 2]) && isxdigit(s[sindex + 3]) )
153 + /* carefully remember to call tolower here... */
154 + result[rrindex] = tolower( hex2dec(s[sindex + 2])*16 +
155 + hex2dec(s[sindex + 3] ) );
157 + switch ( result[rrindex] )
172 + "Warning: layer7 regexp contains a control character, %c, in hex (\\x%c%c).\n"
173 + "I recommend that you write this as %c or \\%c, depending on what you meant.\n",
174 + result[rrindex], s[sindex + 2], s[sindex + 3], result[rrindex], result[rrindex]);
178 + "Warning: null (\\x00) in layer7 regexp. A null terminates the regexp string!\n");
185 + sindex += 3; /* 4 total */
188 + result[rrindex] = tolower(s[sindex]);
193 + result[rrindex] = '\0';
198 +#define MAX_SUBDIRS 128
199 +static char ** readl7dir(char * dirname)
202 + struct dirent ** namelist;
203 + char ** subdirs = malloc(MAX_SUBDIRS * sizeof(char *));
208 + n = scandir(dirname, &namelist, 0, alphasort);
213 + exit_error(OTHER_PROBLEM, "Couldn't open %s\n", dirname);
219 + char fulldirname[MAX_FN_LEN];
221 + snprintf(fulldirname, MAX_FN_LEN, "%s/%s", dirname, namelist[n]->d_name);
223 + if((scratchdir = opendir(fulldirname)) != NULL)
225 + closedir(scratchdir);
227 + if(!strcmp(namelist[n]->d_name, ".") ||
228 + !strcmp(namelist[n]->d_name, ".."))
232 + subdirs[d] = malloc(strlen(namelist[n]->d_name) + 1);
233 + strcpy(subdirs[d], namelist[n]->d_name);
235 + if(d >= MAX_SUBDIRS - 1)
238 + "Too many subdirectories, skipping the rest!\n");
253 +static void parse_layer7_protocol(const char *s, struct xt_layer7_info *info)
255 + char filename[MAX_FN_LEN];
258 + int n = 0, done = 0;
260 + if(strlen(l7dir) > 0) dir = l7dir;
261 + else dir = "/etc/l7-protocols";
263 + subdirs = readl7dir(dir);
265 + while(subdirs[n] != NULL)
267 + int c = snprintf(filename, MAX_FN_LEN, "%s/%s/%s.pat", dir, subdirs[n], s);
270 + exit_error(OTHER_PROBLEM,
271 + "Filename beginning with %s is too long!\n", filename);
273 + /* read in the pattern from the file */
274 + if(parse_protocol_file(filename, s, info)){
283 + exit_error(OTHER_PROBLEM,
284 + "Couldn't find a pattern definition file for %s.\n", s);
286 + /* process \xHH escapes and tolower everything. (our regex lib has no
287 + case insensitivity option.) */
288 + strncpy(info->pattern, pre_process(info->pattern), MAX_PATTERN_LEN);
291 +/* Function which parses command options; returns true if it ate an option */
292 +static int parse(int c, char **argv, int invert, unsigned int *flags,
293 + const void *entry, struct xt_entry_match **match)
295 + struct xt_layer7_info *layer7info =
296 + (struct xt_layer7_info *)(*match)->data;
300 + parse_layer7_protocol(argv[optind-1], layer7info);
302 + layer7info->invert = true;
307 + if(strlen(argv[optind-1]) >= MAX_FN_LEN)
308 + exit_error(PARAMETER_PROBLEM, "directory name too long\n");
310 + strncpy(l7dir, argv[optind-1], MAX_FN_LEN);
322 +/* Final check; must have specified --l7proto */
323 +static void final_check(unsigned int flags)
326 + exit_error(PARAMETER_PROBLEM,
327 + "LAYER7 match: You must specify `--l7proto'");
330 +static void print_protocol(char s[], int invert, int numeric)
332 + fputs("l7proto ", stdout);
333 + if (invert) fputc('!', stdout);
337 +/* Prints out the matchinfo. */
338 +static void print(const void *ip,
339 + const struct xt_entry_match *match,
343 + print_protocol(((struct xt_layer7_info *)match->data)->protocol,
344 + ((struct xt_layer7_info *)match->data)->invert, numeric);
346 +/* Saves the union ipt_matchinfo in parsable form to stdout. */
347 +static void save(const void *ip, const struct xt_entry_match *match)
349 + const struct xt_layer7_info *info =
350 + (const struct xt_layer7_info*) match->data;
352 + printf("--l7proto %s%s ", (info->invert)? "! ":"", info->protocol);
355 +static struct xtables_match layer7 = {
358 + .version = XTABLES_VERSION,
359 + .size = XT_ALIGN(sizeof(struct xt_layer7_info)),
360 + .userspacesize = XT_ALIGN(sizeof(struct xt_layer7_info)),
363 + .final_check = &final_check,
371 + xtables_register_match(&layer7);
373 diff -Nur a/libxt_layer7.man b/libxt_layer7.man
374 --- a/libxt_layer7.man 1970-01-01 01:00:00.000000000 +0100
375 +++ b/libxt_layer7.man 2008-08-22 16:00:52.000000000 +0200
377 +This module matches packets based on the application layer data of
378 +their connections. It uses regular expression matching to compare
379 +the application layer data to regular expressions found it the layer7
380 +configuration files. This is an experimental module which can be found at
381 +http://l7-filter.sf.net. It takes two options.
383 +.BI "--l7proto " "\fIprotocol\fP"
384 +Match the specified protocol. The protocol name must match a file
385 +name in /etc/l7-protocols/ or one of its first-level child directories.
387 +.BI "--l7dir " "\fIdirectory\fP"
388 +Use \fIdirectory\fP instead of /etc/l7-protocols/. This option must be
389 +specified before --l7proto.