blob: dbed67db3a0d895f92da710020b12f65d33f262b [file] [log] [blame]
Emmanuel Roger30719132000-10-04 15:19:31 +00001/* Shared library add-on to iptables to add string matching support.
2 *
3 * Copyright (C) 2000 Emmanuel Roger <winfield@freegates.be>
András Kis-Szabó764316a2001-02-26 17:31:20 +00004 *
5 * ChangeLog
6 * 27.01.2001: Gianni Tedesco <gianni@ecsc.co.uk>
7 * Changed --tos to --string in save(). Also
8 * updated to work with slightly modified
9 * ipt_string_info.
Emmanuel Roger30719132000-10-04 15:19:31 +000010 */
11#include <stdio.h>
12#include <netdb.h>
13#include <string.h>
14#include <stdlib.h>
15#include <getopt.h>
Michael Rashf8ac3292003-02-26 17:34:13 +000016#include <ctype.h>
Emmanuel Roger30719132000-10-04 15:19:31 +000017
18#include <iptables.h>
19#include <linux/netfilter_ipv4/ipt_string.h>
20
21/* Function which prints out usage message. */
22static void
23help(void)
24{
25 printf(
26"STRING match v%s options:\n"
27"--string [!] string Match a string in a packet\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000028IPTABLES_VERSION);
Emmanuel Roger30719132000-10-04 15:19:31 +000029
30 fputc('\n', stdout);
31}
32
33static struct option opts[] = {
34 { "string", 1, 0, '1' },
35 {0}
36};
37
38/* Initialize the match. */
39static void
40init(struct ipt_entry_match *m, unsigned int *nfcache)
41{
42 *nfcache |= NFC_UNKNOWN;
43}
44
45static void
46parse_string(const unsigned char *s, struct ipt_string_info *info)
47{
Michael Rashf8ac3292003-02-26 17:34:13 +000048 int i=0, slen, sindex=0, schar;
49 short hex_f = 0, literal_f = 0;
50 char hextmp[3];
51
52 slen = strlen(s);
53
54 if (slen == 0) {
55 exit_error(PARAMETER_PROBLEM,
56 "STRING must contain at least one char");
57 }
58
59 while (i < slen) {
60 if (s[i] == '\\' && !hex_f) {
61 literal_f = 1;
62 } else if (s[i] == '\\') {
63 exit_error(PARAMETER_PROBLEM,
64 "Cannot include literals in hex data");
65 } else if (s[i] == '|') {
66 if (hex_f)
67 hex_f = 0;
68 else
69 hex_f = 1;
70 if (i+1 >= slen)
71 break;
72 else
73 i++; /* advance to the next character */
74 }
75
76 if (literal_f) {
77 if (i+1 >= slen) {
78 exit_error(PARAMETER_PROBLEM,
79 "Bad literal placement at end of string");
80 }
81 info->string[sindex] = s[i+1];
82 i += 2; /* skip over literal char */
83 literal_f = 0;
84 } else if (hex_f) {
85 if (i+1 >= slen) {
86 exit_error(PARAMETER_PROBLEM,
87 "Odd number of hex digits");
88 }
89 if (i+2 >= slen) {
90 /* must end with a "|" */
91 exit_error(PARAMETER_PROBLEM, "Invalid hex block");
92 }
93 hextmp[0] = s[i];
94 hextmp[1] = s[i+1];
95 hextmp[2] = '\0';
96 if (! sscanf(hextmp, "%x", &schar))
97 exit_error(PARAMETER_PROBLEM,
98 "Invalid hex char `%c'", s[i]);
99 info->string[sindex] = (char) schar;
100 if (s[i+2] == ' ')
101 i += 3; /* spaces included in the hex block */
102 else
103 i += 2;
104 } else { /* the char is not part of hex data, so just copy */
105 info->string[sindex] = s[i];
106 i++;
107 }
108 if (sindex > BM_MAX_NLEN)
109 exit_error(PARAMETER_PROBLEM, "STRING too long `%s'", s);
110 sindex++;
111 }
Emmanuel Roger30719132000-10-04 15:19:31 +0000112}
113
114/* Function which parses command options; returns true if it
115 ate an option */
116static int
117parse(int c, char **argv, int invert, unsigned int *flags,
118 const struct ipt_entry *entry,
119 unsigned int *nfcache,
120 struct ipt_entry_match **match)
121{
122 struct ipt_string_info *stringinfo = (struct ipt_string_info *)(*match)->data;
123
124 switch (c) {
125 case '1':
Harald Welteb77f1da2002-03-14 11:35:58 +0000126 check_inverse(optarg, &invert, &optind, 0);
Emmanuel Roger30719132000-10-04 15:19:31 +0000127 parse_string(argv[optind-1], stringinfo);
128 if (invert)
129 stringinfo->invert = 1;
András Kis-Szabó764316a2001-02-26 17:31:20 +0000130 stringinfo->len=strlen((char *)&stringinfo->string);
Emmanuel Roger30719132000-10-04 15:19:31 +0000131 *flags = 1;
132 break;
133
134 default:
135 return 0;
136 }
137 return 1;
138}
139
140static void
141print_string(char string[], int invert, int numeric)
142{
143
144 if (invert)
145 fputc('!', stdout);
146 printf("%s ",string);
147}
148
149/* Final check; must have specified --string. */
150static void
151final_check(unsigned int flags)
152{
153 if (!flags)
154 exit_error(PARAMETER_PROBLEM,
155 "STRING match: You must specify `--string'");
156}
157
158/* Prints out the matchinfo. */
159static void
160print(const struct ipt_ip *ip,
161 const struct ipt_entry_match *match,
162 int numeric)
163{
164 printf("STRING match ");
165 print_string(((struct ipt_string_info *)match->data)->string,
166 ((struct ipt_string_info *)match->data)->invert, numeric);
167}
168
169/* Saves the union ipt_matchinfo in parsable form to stdout. */
170static void
171save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
172{
András Kis-Szabó764316a2001-02-26 17:31:20 +0000173 printf("--string ");
Emmanuel Roger30719132000-10-04 15:19:31 +0000174 print_string(((struct ipt_string_info *)match->data)->string,
175 ((struct ipt_string_info *)match->data)->invert, 0);
176}
177
Harald Welte3efb6ea2001-08-06 18:50:21 +0000178static
Emmanuel Roger30719132000-10-04 15:19:31 +0000179struct iptables_match string
180= { NULL,
181 "string",
Harald Welte80fe35d2002-05-29 13:08:15 +0000182 IPTABLES_VERSION,
Emmanuel Roger30719132000-10-04 15:19:31 +0000183 IPT_ALIGN(sizeof(struct ipt_string_info)),
184 IPT_ALIGN(sizeof(struct ipt_string_info)),
185 &help,
186 &init,
187 &parse,
188 &final_check,
189 &print,
190 &save,
191 opts
192};
193
194void _init(void)
195{
196 register_match(&string);
197}