summaryrefslogtreecommitdiffstats
path: root/KMFIPTInterface/iptchecker.cpp
blob: 9781c4c418ef60451267365e092d563aee8be8d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/***************************************************************************
 *   Copyright (C) 2005 by Christian Hubinger   *
 *   e9806056@student.tuwien.ac.at   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/
#include "iptchecker.h"


extern "C" {
#include <getopt.h>
#include <sys/errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <time.h>
#include <netdb.h>
#include "libiptc/libiptc.h"
#include "iptables.h"
}


/* Here begins some of the code taken from iptables-save.c **************** */
#define IP_PARTS_NATIVE(n)      \
 (unsigned int)((n)>>24)&0xFF,   \
 (unsigned int)((n)>>16)&0xFF,   \
 (unsigned int)((n)>>8)&0xFF,    \
 (unsigned int)((n)&0xFF)



IPTChecker::IPTChecker()
{
}


IPTChecker::~IPTChecker()
{
}

int IPTChecker::numChainsInTable( char *tablename ) {
	int foundChain = 0;
	iptc_handle_t h;
	const char *chain = NULL;

	h = iptc_init(tablename);
	if (!h) {
     	printf("Can't initialize IPT Handle for table %s : %s\n", tablename, iptc_strerror(errno));
		return -1;
	}
	
 	for (chain = iptc_first_chain(&h); chain; chain = iptc_next_chain(&h))  {
       	foundChain++;
  	}
	printf("Table %s Num: %d\n", tablename, foundChain );
	return foundChain;
}


int IPTChecker::numRulesInChain( char *tablename, char *chain ) {
	int foundRule = 0;
	iptc_handle_t h;
  	const struct ipt_entry *e;
	const char *ch = NULL;

	h = iptc_init(tablename);
	if (!h) {
     	printf("Can't initialize IPT Handle for table %s : %s\n", tablename, iptc_strerror(errno));
		return -1;
	}
	
 	if ( iptc_is_chain( chain, h ) == 0 ) {
		printf("Error Chain: %s Not Found in table: %s\n", chain, tablename );
		return -1;
	}	
	for (ch = iptc_first_chain(&h); ch; ch = iptc_next_chain(&h))  {
		if ( strcmp( ch, chain ) == 0 ) {
			int found = 0;
			for (e = iptc_first_rule(ch, &h); e && found == 0; e = iptc_next_rule(e, &h))  {
				foundRule++;
			}
			printf("Table: %s Chain: %s Num Rules: %d\n", tablename, chain, foundRule );
			return foundRule;
		}
   	}
	return -1;
}
TQStringList IPTChecker::getRuleProperties( char * table, char *chain, int ruleIndex ) {
	TQStringList list;
	int numRules = 0;
	int found = 0;
	iptc_handle_t h;
  	const struct ipt_entry *rule = 0;
	const struct ipt_entry *my_rule = 0;
	const char *ch = NULL;

	h = iptc_init(table);
	if (!h) {
     	printf("Can't initialize IPT Handle for table %s : %s\n", table, iptc_strerror(errno));
		return list;
	}
	
 	if ( iptc_is_chain( chain, h ) == 0 ) {
		printf("Error Chain: %s Not Found in table: %s\n", chain, table );
		return list;
	}	
	int foundChain = 0;
	for (ch = iptc_first_chain(&h); ch && foundChain == 0; ch = iptc_next_chain(&h))  {
		if ( strcmp( ch, chain ) == 0 ) {
			foundChain = 1;
			for (rule = iptc_first_rule(ch, &h); rule && found == 0; rule = iptc_next_rule(rule, &h))  {
				if ( numRules == ruleIndex ) {
					my_rule = rule;
					found = 1;
				}	
				numRules++;
			}
		}
   	}
	if ( my_rule ) {
		const char *target_name;
		/* Print target name */	
		target_name = iptc_get_target( my_rule, &h );
		if ( target_name && (*target_name != '\0') ) {
			
			list << TQString("%1").arg(target_name);
		}
		

		/* Print targinfo part */
/*		struct ipt_entry_target *t;
		t = ipt_get_target( (struct ipt_entry *) my_rule );
		if ( t ) {
			list << "Found Target:";
			list <<  t->u.user.name;
		}

		if ( t->u.user.name ) {
			list << TQString("TARGET2:%1").arg(t->u.user.name);
			
			struct iptables_target *target = find_target( target_name, TRY_LOAD );
			
			if ( ! target ) {
				list << "Can't find library for target `%s'\n" << t->u.user.name;
				return list;
			}
			
			if (target->save) {
				target->save(&my_rule->ip, t);
			} else {
				// If the target size is greater than ipt_entry_target
				// there is something to be saved, we just don't know
				// how to print it 
				if (t->u.target_size != sizeof(struct ipt_entry_target)) {
					fprintf(stderr, "Target `%s' is missing "
						"save function\n",
					t->u.user.name);
					return list;
				} 
			}
		}*/
	} else {
		list << "ERROR";
	}
	return list;
}

TQStringList IPTChecker::getChainsInTable( char * table ) {
	TQStringList list;
	int foundRule = 0;
	iptc_handle_t h;
  	const struct ipt_entry *e;
	const char *chain = NULL;

	h = iptc_init(table);
	if (!h) {
     	printf("Can't initialize IPT Handle for table %s : %s\n", table, iptc_strerror(errno));
		return list;
	}
	
 	
	for ( chain = iptc_first_chain(&h); chain; chain = iptc_next_chain(&h) )  {
       	list << chain;
  	}
	return list;
}


TQString IPTChecker::getChainPolicy( char * table, char *chain ) {
	TQString policy = "";
	iptc_handle_t h;
  	const struct ipt_entry *e;
	const char *ch = NULL;

	h = iptc_init(table);
	if (!h) {
     	printf("Can't initialize IPT Handle for table %s : %s\n", table, iptc_strerror(errno));
		return "";
	}
	
 	
	for ( ch = iptc_first_chain(&h); ch; ch = iptc_next_chain(&h) )  {
		if ( strcmp( ch, chain ) == 0 ) {
			
		if ( iptc_builtin(chain, h) ) {
				struct ipt_counters count;
				policy = iptc_get_policy(chain, &count, &h);
				
/*				printf("%s ",
				       iptc_get_policy(chain, &count, &h));
				printf("[%llu:%llu]\n", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);*/
			} else {
// 				printf("- [0:0]\n");
			}

		}
  	}
	return policy;
}