blob: 0ffa365f5ff5ef7a2b3e53618b8fcbbdaf4a3ac0 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Shared library add-on to iptables to add string matching support.
2 *
3 * (C) 2010 Atheros Communications. Inc.
4 * 3/26/2010: <yingming.yu@atheros.com>
5 * Port to netfilter core interface of 2.6.31 kernel (ipt->xt)
6 *
7 * Copyright (C) 2000 Emmanuel Roger <winfield@freegates.be>
8 *
9 * ChangeLog
10 * 27.01.2001: Gianni Tedesco <gianni@ecsc.co.uk>
11 * Changed --tos to --string in save(). Also
12 * updated to work with slightly modified
13 * ipt_string_info.
14 */
15
16/* Shared library add-on to iptables to add webstr matching support.
17 *
18 * Copyright (C) 2003, CyberTAN Corporation
19 * All Rights Reserved.
20 *
21 * Description:
22 * This is shared library, added to iptables, for web content inspection.
23 * It was derived from 'string' matching support, declared as above.
24 *
25 */
26
27
28#include <stdio.h>
29#include <netdb.h>
30#include <string.h>
31#include <stdlib.h>
32#include <getopt.h>
33
34#include <xtables.h>
35#include <linux/netfilter/x_tables.h>
36#include <linux/netfilter/xt_webstr.h>
37
38/* Function which prints out usage message. */
39static void
40help(void)
41{
42 printf(
43"WEBSTR match v%s options:\n"
44"--webstr [!] host Match a http string in a packet\n"
45"--webstr [!] url Match a http string in a packet\n"
46"--webstr [!] content Match a http string in a packet\n",
47XTABLES_VERSION);
48
49 fputc('\n', stdout);
50}
51
52static struct option opts[] = {
53 { "host", 1, 0, '1' },
54 { "url", 1, 0, '2' },
55 { "content", 1, 0, '3' },
56 {0}
57};
58
59/* Initialize the match. */
60static void
61init(struct xt_entry_match *m, unsigned int *nfcache)
62{
63 *nfcache |= NFC_UNKNOWN;
64}
65
66static void
67parse_string(const char *s, struct xt_webstr_info *info)
68{
69 if (strlen(s) <= BM_MAX_NLEN) strcpy(info->string, s);
70 else xtables_error(PARAMETER_PROBLEM, "WEBSTR too long `%s'", s);
71}
72
73/* Function which parses command options; returns true if it
74 ate an option */
75static int
76parse(int c, char **argv, int invert, unsigned int *flags,
77 const void *entry,
78 struct xt_entry_match **match)
79{
80 struct xt_webstr_info *stringinfo = (struct xt_webstr_info *)(*match)->data;
81
82 switch (c) {
83 case '1':
84 xtables_check_inverse(optarg, &invert, &optind, 0);
85 parse_string(argv[optind-1], stringinfo);
86 if (invert)
87 stringinfo->invert = 1;
88 stringinfo->len=strlen((char *)&stringinfo->string);
89 stringinfo->type = IPT_WEBSTR_HOST;
90 break;
91
92 case '2':
93 xtables_check_inverse(optarg, &invert, &optind, 0);
94 parse_string(argv[optind-1], stringinfo);
95 if (invert)
96 stringinfo->invert = 1;
97 stringinfo->len=strlen((char *)&stringinfo->string);
98 stringinfo->type = IPT_WEBSTR_URL;
99 break;
100
101 case '3':
102 xtables_check_inverse(optarg, &invert, &optind, 0);
103 parse_string(argv[optind-1], stringinfo);
104 if (invert)
105 stringinfo->invert = 1;
106 stringinfo->len=strlen((char *)&stringinfo->string);
107 stringinfo->type = IPT_WEBSTR_CONTENT;
108 break;
109
110 default:
111 return 0;
112 }
113
114 *flags = 1;
115 return 1;
116}
117
118static void
119print_string(char string[], int invert, int numeric)
120{
121
122 if (invert)
123 fputc('!', stdout);
124 printf("%s ",string);
125}
126
127/* Final check; must have specified --string. */
128static void
129final_check(unsigned int flags)
130{
131 if (!flags)
132 xtables_error(PARAMETER_PROBLEM,
133 "WEBSTR match: You must specify `--webstr'");
134}
135
136/* Prints out the matchinfo. */
137static void
138print(const void *ip,
139 const struct xt_entry_match *match,
140 int numeric)
141{
142 struct xt_webstr_info *stringinfo = (struct xt_webstr_info *)match->data;
143
144 printf("WEBSTR match ");
145
146
147 switch (stringinfo->type) {
148 case IPT_WEBSTR_HOST:
149 printf("host ");
150 break;
151
152 case IPT_WEBSTR_URL:
153 printf("url ");
154 break;
155
156 case IPT_WEBSTR_CONTENT:
157 printf("content ");
158 break;
159
160 default:
161 printf("ERROR ");
162 break;
163 }
164
165 print_string(((struct xt_webstr_info *)match->data)->string,
166 ((struct xt_webstr_info *)match->data)->invert, numeric);
167}
168
169/* Saves the union ipt_matchinfo in parsable form to stdout. */
170static void
171save(const void *ip, const struct xt_entry_match *match)
172{
173 printf("--webstr ");
174 print_string(((struct xt_webstr_info *)match->data)->string,
175 ((struct xt_webstr_info *)match->data)->invert, 0);
176}
177
178static struct xtables_match webstr = {
179 .next = NULL,
180 .name = "webstr",
181 .version = XTABLES_VERSION,
182 .size = XT_ALIGN(sizeof(struct xt_webstr_info)),
183 .userspacesize = XT_ALIGN(sizeof(struct xt_webstr_info)),
184 .help = &help,
185 //.init = init,
186 .parse = parse,
187 .final_check = final_check,
188 .print = print,
189 .save = save,
190 .extra_opts = opts
191};
192
193void _init(void)
194{
195 xtables_register_match(&webstr);
196}