iptables-1.2.9-2.3.1.src.rpm
[iptables.git] / extensions / libipt_osf.c
1 /*
2  * libipt_osf.c
3  *
4  * Copyright (c) 2003 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5  *
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 /*
23  * iptables interface for OS fingerprint matching module.
24  */
25
26 #include <stdio.h>
27 #include <netdb.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <getopt.h>
31 #include <ctype.h>
32
33 #include <iptables.h>
34 #include <linux/netfilter_ipv4/ipt_osf.h>
35
36 static void help(void)
37 {
38         printf("OS fingerprint match v%s options:\n"
39                 "  --genre [!] string          Match a OS genre bypassive fingerprinting.\n",
40                 IPTABLES_VERSION);
41 }
42
43
44 static struct option opts[] = {
45         { .name = "genre",     .has_arg = 1, .flag = 0, .val = '1' },
46         { .name = 0 }
47 };
48
49
50 static void init(struct ipt_entry_match *m, unsigned int *nfcache)
51 {
52         *nfcache |= NFC_UNKNOWN;
53 }
54
55
56 static void parse_string(const unsigned char *s, struct ipt_osf_info *info)
57 {
58         if (strlen(s) < MAXGENRELEN) 
59                 strcpy(info->genre, s);
60         else 
61                 exit_error(PARAMETER_PROBLEM, "Genre string too long `%s' [%d], max=%d", 
62                                 s, strlen(s), MAXGENRELEN);
63 }
64
65 static int parse(int c, char **argv, int invert, unsigned int *flags,
66                         const struct ipt_entry *entry,
67                         unsigned int *nfcache,
68                         struct ipt_entry_match **match)
69 {
70         struct ipt_osf_info *info = (struct ipt_osf_info *)(*match)->data;
71         
72         switch(c) 
73         {
74                 case '1':
75                         if (*flags)
76                                 exit_error(PARAMETER_PROBLEM, "Can't specify multiple strings");
77                         check_inverse(optarg, &invert, &optind, 0);
78                         parse_string(argv[optind-1], info);
79                         if (invert)
80                                 info->invert = 1;
81                         info->len=strlen((char *)info->genre);
82                         *flags = 1;
83                         break;
84                 default:
85                         return 0;
86         }
87
88         return 1;
89 }
90
91 static void final_check(unsigned int flags)
92 {
93         if (!flags)
94                 exit_error(PARAMETER_PROBLEM, "OS fingerprint match: You must specify `--genre'");
95 }
96
97 static void print(const struct ipt_ip *ip, const struct ipt_entry_match *match, int numeric)
98 {
99         const struct ipt_osf_info *info = (const struct ipt_osf_info*) match->data;
100
101         printf("OS fingerprint match %s%s ", (info->invert) ? "!" : "", info->genre);
102 }
103
104 static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
105 {
106         const struct ipt_osf_info *info = (const struct ipt_osf_info*) match->data;
107
108         printf("--genre %s%s ", (info->invert) ? "! ": "", info->genre);
109 }
110
111
112 static struct iptables_match osf_match = {
113     .name          = "osf",
114     .version       = IPTABLES_VERSION,
115     .size          = IPT_ALIGN(sizeof(struct ipt_osf_info)),
116     .userspacesize = IPT_ALIGN(sizeof(struct ipt_osf_info)),
117     .help          = &help,
118     .init          = &init,
119     .parse         = &parse,
120     .final_check   = &final_check,
121     .print         = &print,
122     .save          = &save,
123     .extra_opts    = opts
124 };
125
126
127 void _init(void)
128 {
129         register_match(&osf_match);
130 }