| Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 1 | /* 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ó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 4 | * |
| 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 Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 10 | */ |
| 11 | #include <stdio.h> |
| 12 | #include <netdb.h> |
| 13 | #include <string.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <getopt.h> |
| Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame^] | 16 | #include <ctype.h> |
| Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 17 | |
| 18 | #include <iptables.h> |
| 19 | #include <linux/netfilter_ipv4/ipt_string.h> |
| 20 | |
| 21 | /* Function which prints out usage message. */ |
| 22 | static void |
| 23 | help(void) |
| 24 | { |
| 25 | printf( |
| 26 | "STRING match v%s options:\n" |
| 27 | "--string [!] string Match a string in a packet\n", |
| Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 28 | IPTABLES_VERSION); |
| Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 29 | |
| 30 | fputc('\n', stdout); |
| 31 | } |
| 32 | |
| 33 | static struct option opts[] = { |
| 34 | { "string", 1, 0, '1' }, |
| 35 | {0} |
| 36 | }; |
| 37 | |
| 38 | /* Initialize the match. */ |
| 39 | static void |
| 40 | init(struct ipt_entry_match *m, unsigned int *nfcache) |
| 41 | { |
| 42 | *nfcache |= NFC_UNKNOWN; |
| 43 | } |
| 44 | |
| 45 | static void |
| 46 | parse_string(const unsigned char *s, struct ipt_string_info *info) |
| 47 | { |
| Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame^] | 48 | 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 Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* Function which parses command options; returns true if it |
| 115 | ate an option */ |
| 116 | static int |
| 117 | parse(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 Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 126 | check_inverse(optarg, &invert, &optind, 0); |
| Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 127 | parse_string(argv[optind-1], stringinfo); |
| 128 | if (invert) |
| 129 | stringinfo->invert = 1; |
| András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 130 | stringinfo->len=strlen((char *)&stringinfo->string); |
| Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 131 | *flags = 1; |
| 132 | break; |
| 133 | |
| 134 | default: |
| 135 | return 0; |
| 136 | } |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | static void |
| 141 | print_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. */ |
| 150 | static void |
| 151 | final_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. */ |
| 159 | static void |
| 160 | print(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. */ |
| 170 | static void |
| 171 | save(const struct ipt_ip *ip, const struct ipt_entry_match *match) |
| 172 | { |
| András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 173 | printf("--string "); |
| Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 174 | print_string(((struct ipt_string_info *)match->data)->string, |
| 175 | ((struct ipt_string_info *)match->data)->invert, 0); |
| 176 | } |
| 177 | |
| Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 178 | static |
| Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 179 | struct iptables_match string |
| 180 | = { NULL, |
| 181 | "string", |
| Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 182 | IPTABLES_VERSION, |
| Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 183 | 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 | |
| 194 | void _init(void) |
| 195 | { |
| 196 | register_match(&string); |
| 197 | } |