summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/core/kmfprotocol.cpp
blob: 9c208b523a11a21418ceb835f511050315ae4f4d (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//
// C++ Implementation: $MODULE$
//
// Description:
//
//
// Author: Christian Hubinger <chubinger@irrsinnig.org>, (C) 2003
//
// Copyright: See COPYING file that comes with this distribution
//
//
/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/


#include "kmfprotocol.h"

// TQt includes
#include <tqfile.h>
#include <tqdir.h>
#include <tqdom.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqvaluelist.h>

// KDE includes
#include <kdebug.h>
#include <kstandarddirs.h>
#include <tdelocale.h>
#include <tdeio/netaccess.h>
#include <tdeio/job.h>
#include <ktrader.h>
#include <klibloader.h>
#include <tdetempfile.h>
#include <tdefileitem.h>

// project includes
#include "../version.h"
#include "xmlnames.h"
#include "kmfundoengine.h"
#include "kmferror.h"
#include "kmferrorhandler.h"
#include "kmfgenericdoc.h"
#include "kmfnetzone.h"
#include "kmfnethost.h"
#include "kmfprotocolcategory.h"
#include "kmfprotocolusage.h"

namespace KMF {

KMFProtocol::KMFProtocol( KMFProtocolCategory* protCat, const char* name ) : NetfilterObject( protCat, name ) {
	// kdDebug() << "KMFProtocol::KMFProtocol( NetfilterObject* parent, const char* name )" << endl;
	m_category = protCat;
	m_customProtocol = true;
	m_tcpPorts.clear();
	m_udpPorts.clear();
/*	m_udpPorts = new TQValueList<int>;
	m_tcpPorts = new TQValueList<int>;*/
}


KMFProtocol::~KMFProtocol() {
	kdDebug() << "KMFProtocol::~KMFProtocol()" << endl;
	m_tcpPorts.clear();
	m_udpPorts.clear();
	
}

int KMFProtocol::type() {
	// kdDebug() << "KMFProtocol::type()" << endl;
	return NetfilterObject::PROTOCOL;
}

void KMFProtocol::clear() {
	m_tcpPorts.clear();
	m_udpPorts.clear();
}


void KMFProtocol::setCategory( KMFProtocolCategory* protCat ) {
	
	m_category = protCat;

}

const TQString& KMFProtocol::tcpPortsList() {
	kdDebug() << "void KMFProtocol::tcpPortsList()" << endl;
	kdDebug() << "Contains: " << tcpPorts().size() << " ports"  << endl;
	TQStringList *l = new TQStringList();
	TQValueList<int>::iterator it;
    for ( it = tcpPorts().begin(); it != tcpPorts().end(); ++it ) {
		TQString s = "";
		s.setNum( *it );
		*l << s;
	}
	
	return *(new TQString( l->join(",") ) );
}

const TQString& KMFProtocol::udpPortsList() {
	kdDebug() << "void KMFProtocol::udpPortsList()" << endl;
	kdDebug() << "Contains: " << m_udpPorts.size() << " ports"  << endl;
	TQStringList *l = new TQStringList();
	TQValueList<int>::iterator it;
     for ( it = m_udpPorts.begin(); it != m_udpPorts.end(); ++it ) {
		TQString s = "";
		s.setNum( *it );
		*l << s;
	}
	return *(new TQString( l->join(",") ) );
}

void KMFProtocol::addPort( const TQString& port, int protocol ) {
	// kdDebug() << "void KMFProtocol::addPort( const TQString& )" << endl;
	if ( protocol == UDP && udpPorts().contains( port.toInt() ) == 0 ) {
		kdDebug() << " + + + Register UDP Port:" <<  port << endl;
		udpPorts().append( port.toInt() );
		qHeapSort( udpPorts() );
		changed();
		return;
	} 
	if ( protocol == TCP && tcpPorts().contains( port.toInt() ) == 0 ) {
		kdDebug() << " + + + Register TCP Port:" <<  port << endl;
		tcpPorts().append( port.toInt() );
		qHeapSort( tcpPorts() );
		changed();
		return;
	}
	kdDebug() << "WARNING: ignoring duplicate port entry: " << port << " in protocol: " << name() << endl;
	
}

void KMFProtocol::delPort( const TQString& port, int protocol  ) {
	kdDebug() << "void KMFProtocol::delPort( const TQString& )" << endl;
	if ( protocol == UDP && udpPorts().contains( port.toInt() ) > 0  ) {
		kdDebug() << "KMFProtocol: " << name() << " Unregister UDP Port:" <<  port << endl;
		udpPorts().remove( udpPorts().find( port.toInt() ) );
		qHeapSort( udpPorts() );
		changed();
	} else if ( protocol == TCP && tcpPorts().contains( port.toInt() ) > 0 ) {
		kdDebug() << "KMFProtocol: " << name() << " Unregister TCP Port:" <<  port << endl;
		tcpPorts().remove( tcpPorts().find( port.toInt() ) );
		qHeapSort( tcpPorts() );
		changed();
	}  else {
		kdDebug() << "WARNING: no entry found to remove port: " << port  << " from protocol: " << name() << endl;
	}
}

void KMFProtocol::setCustomProtocol( bool onoff ){
// 	kdDebug() << "void KMFProtocol::setCustomProtocol( bool )" << endl;
	if( onoff != m_customProtocol ) {
		m_customProtocol = onoff;
		changed();
	}
}

bool KMFProtocol::isEquivalent( KMFProtocol *other ){
	kdDebug() << "void KMFProtocol::isEquivalent( KMFProtocol& other )" << endl;
	kdDebug() << "Comparing: "  << name() << " with: " << other->name() << endl;
	if ( tcpPorts().size() != other->tcpPorts().size() ) {
		kdDebug() << "Have different TCP port count." << endl;
		return false;
	}
	
	TQValueList<int>::iterator itTcp;
	for( itTcp = tcpPorts().begin(); itTcp != tcpPorts().end(); ++itTcp ) {
		if ( other->tcpPorts().contains( *itTcp ) == 0 ) {
			kdDebug() << "TCP port " << *itTcp << " not found in other protocol." << endl;
			return false;
		}
	}
	
	if ( udpPorts().size() != other->udpPorts().size() ) {
		kdDebug() << "Have different UDP port count." << endl;
		return false;
	}
	TQValueList<int>::iterator itUdp;
	for( itUdp = udpPorts().begin(); itUdp != udpPorts().end(); ++itUdp ) {
		if ( other->udpPorts().contains( *itUdp ) == 0 ) {
			kdDebug() << "UDP port " << *itUdp << " not found in other protocol." << endl;
			return false;
		}
	}
	kdDebug() << "Protocol: "  << name() << " is Equivalent to protocol: " << other->name() << endl;
	return true;
}

bool KMFProtocol::replaceTCPPort( int oldPort, int newPort ){
	kdDebug() << "void KMFProtocol::replaceTCPPort( int " << oldPort  << ", int " << newPort << " )" << endl;
	
	if ( tcpPorts().contains( newPort ) > 0 ) {
		kdDebug() << "WARNING: ignoring duplicate port entry: " << newPort << " in protocol: " << name() << endl;
		return false;
	}
	
	int index =  tcpPorts().findIndex( oldPort );
	kdDebug() << "Found Port at: " << index << endl;
	if ( index == -1 ) {
		kdDebug() << "WARNING: port entry: " << oldPort << "not found  in protocol: " << name() << endl;
		return false;
	} 

	*tcpPorts().at( index ) = newPort;
	qHeapSort( tcpPorts() );
	changed();
	return true;
}
bool KMFProtocol::replaceUDPPort( int oldPort, int newPort ){
	kdDebug() << "void KMFProtocol::replaceUDPPort( int " << oldPort  << ", int " << newPort << " )" << endl;
	
	if ( m_udpPorts.contains( newPort ) > 0 ) {
		kdDebug() << "WARNING: ignoring duplicate port entry: " << newPort << " in protocol: " << name() << endl;
		return false;
	}
	
	int index =  udpPorts().findIndex( oldPort );
	kdDebug() << "Found Port at: " << index << endl;
	if ( index == -1 ) {
		kdDebug() << "WARNING: port entry: " << oldPort << "not found  in protocol: " << name() << endl;
		return false;
	} 
	
	*udpPorts().at( index ) = newPort;
	qHeapSort( m_udpPorts );
	changed();
	return true;
}

KMFProtocolUsage *KMFProtocol::createUsage() {
	KMFProtocolUsage *use = new KMFProtocolUsage( this, "KMFProtocolUsage" );
	use->setProtocol( this );
	m_usages.append( use );
	return use;
}

const TQDomDocument& KMFProtocol::getDOMTree() {
// 	kdDebug() << "const TQDomDocument& KMFProtocol::getDOMTree()" << endl;
	TQDomDocument doc;
	TQDomElement root = doc.createElement( XML::Protocol_Element );
	NetfilterObject::saveUuid( root );
	
	root.setAttribute(  XML::Name_Attribute, name() );
	root.setAttribute( XML::Description_Attribute, description() );
	TQValueList<int>::iterator it;
	kdDebug() << "Serializte ports: " << udpPortsList() << endl;
	for ( it = udpPorts().begin(); it != udpPorts().end(); ++it ) {
		TQDomElement port = doc.createElement( XML::Port_Element );
		root.appendChild( port );
		port.setAttribute( XML::Num_Attribute,*it);
		port.setAttribute( XML::Protocol_Attribute , XML::UDP_Value );
	}
	kdDebug() << "Serializte ports: " << tcpPortsList() << endl;
	for (  it = tcpPorts().begin(); it != tcpPorts().end(); ++it ) {
		TQDomElement port = doc.createElement( XML::Port_Element );
		root.appendChild( port );
		port.setAttribute( XML::Num_Attribute,*it);
		port.setAttribute( XML::Protocol_Attribute ,XML::TCP_Value );
	}

	doc.appendChild( root );
	return *( new TQDomDocument( doc ) );
}

void KMFProtocol::loadXML( const TQDomDocument& doc, TQStringList& errors ) {
	// kdDebug() << "void KMFProtocol::loadXML( const TQDomDocument& )" << endl;
 	TQDomElement root = doc.documentElement();
 	loadXML( root, errors );
}
void KMFProtocol::loadXML( TQDomNode root, TQStringList& errors ) {
//	kdDebug() << "void KMFProtocol::loadXML( TQDomNode root )" << endl;

	// Protocols use Fixed Guids
	NetfilterObject::loadUuid( root, errors );
	
	TQString name = "";
	TQString logging = "";
	TQString desc = "";
	TQString limit = "";
	TQString io = "";
	
	name = root.toElement().attribute( XML::Name_Attribute );
	desc = root.toElement().attribute( XML::Description_Attribute );
	
	setDescription( *( new TQString( desc ) ) );
	setName( *(new TQString( name ) ) );
	
	TQDomNode curr = root.firstChild();
	while ( !curr.isNull() ) {
		if ( curr.isElement() && curr.nodeName() == XML::Port_Element ) {
			TQString port = curr.toElement().attribute( XML::Num_Attribute );
			TQString protocol = curr.toElement().attribute( XML::Protocol_Attribute );
			if ( protocol == XML::UDP_Value ) {
				addPort( port, UDP );
			}
			if ( protocol == XML::TCP_Value ) {
				addPort( port, TCP );
			}
		}
		curr = curr.nextSibling();
	}
	changed();
}

}