summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/compilers/pf/kmfpfcompiler.cpp
blob: 9744fe85166cf6e5e55c4c3cda075276703ba706 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//
// C++ Implementation: kmfpfcompiler
//
// Description:
//
//
// Author: Christian Hubinger <e9806056@student.tuwien.ac.at>, (C) 2004
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "kmfpfcompiler.h"

// QT includes
#include <tqptrlist.h>
#include <tqstringlist.h>
#include <tqmultilineedit.h>

// KDE includes
#include <kdebug.h>
#include <tdelocale.h>
#include <tdeaboutdata.h>
#include <tdelocale.h>

// Project includes
#include "../../core/kmfgenericdoc.h"
#include "../../core/kmfiptdoc.h"
#include "../../core/kmfnetzone.h"
#include "../../core/kmfnethost.h"
#include "../../core/kmfprotocol.h"
#include "../../core/kmferror.cpp"
#include "../../core/kmfprotocol.h"
#include "../../core/kmfprotocolusage.h"

#include "../../kmfwidgets/kmflistview.h"
namespace KMF {

KMFPFCompiler::KMFPFCompiler( TQObject* parent, const char* name ) : KMFPlugin( parent, name )  {
	m_osName = "openbsd";
	m_osGUIName = "OpenBSD";
	m_backendName = "pf";
	m_backendGUIName = "PF";

	
	
	if ( genericDoc() ) {
		new TDEAction( i18n( "Export as &PF (OpenBSD) Script" ),  "fileexport",
									0, this, TQ_SLOT( slotExportPF() ), actionCollection(), "compile_pf" );

		setXMLFile( "kmfpfcompiler.rc" );
		kdDebug() << "KMFPFCompiler: Finished initialisation." << endl;
	}
}


KMFPFCompiler::~KMFPFCompiler() {}

const TQString& KMFPFCompiler::osName(){
	return m_osName;
}
const TQString& KMFPFCompiler::osGUIName(){
	return m_osGUIName;
}
const TQString& KMFPFCompiler::backendName(){
	return m_backendName;
}
const TQString& KMFPFCompiler::backendGUIName(){
	return m_backendGUIName;
}


void KMFPFCompiler::compile() {
	kdDebug() << "void KMFPFCompiler::compile()" << endl;
	kdDebug() << "Doc XML:\n"  << m_doc->getXMLSniplet() << endl;
	
}

void KMFPFCompiler::slotExportPF() {
	kdDebug() << "KMFPFCompiler::slotExportIPT()" << endl;
	TQString s = compile( genericDoc() );
	kdDebug() << "Generated scrip: "  << s  << endl;
}

const TQString& KMFPFCompiler::compile( KMFGenericDoc* gendoc ) {
	kdDebug() << "const TQString& KMFPFCompiler::compile( KMFGenericDoc* doc )" << endl;
	if ( gendoc ) {
		m_compiledScript = "";
		
		setupNatRules( gendoc );
		
		setupLocalhostRules();
		
		setupInAndOutHosts( gendoc->trustedHostsZone(), "pass" );
		setupInAndOutHosts( gendoc->maliciousHostsZone(), "block" );
		setupForbiddenHosts( gendoc->badClientsHostsZone(), "in" );
		setupForbiddenHosts( gendoc->badServersHostsZone(), "out" );
		setupICMPRules( gendoc );
		
		createIncommingACCESSRules( 0, gendoc );
		createOutgoingACCESSRules( 0, gendoc );
		setupPolicies( gendoc );
		kdDebug() << "Return compiled pf:\n " << m_compiledScript << endl;
		return m_compiledScript;
	} else {
		return *( new TQString( "ERROR: Couldn't compile document - may be wrong type " ) );
	}
}

void KMFPFCompiler::setupInAndOutHosts( KMFNetZone* zone ,  const TQString& cmd ) {
	kdDebug() << "KMFPFCompiler::setupInAndOutHosts( KMFNetZone* )" << endl;
	if ( zone->hosts().count() == 0 ) {
		return;
	}
	
	TQPtrListIterator<KMFTarget> it ( zone->hosts() );
	if ( cmd == "pass" ) {
		m_compiledScript.append( "\n# Trusted Hosts\n" );
	} else {
		m_compiledScript.append( "\n# Maliciuous Hosts\n" );
	}
	
	while ( it.current() ) {
		KMFNetHost *host = dynamic_cast<KMFNetHost*> ( *it );
		++it;
		
		if ( cmd == "pass" ) {
			m_compiledScript.append( "pass in quick from " );
			m_compiledScript.append( host->address()->toString() );
			m_compiledScript.append( " to any keep state\n" );
		} else {
			m_compiledScript.append( "block in quick from " );
			m_compiledScript.append( host->address()->toString() );
			m_compiledScript.append( " to any\n" );
		}
	}
}

void KMFPFCompiler::setupForbiddenHosts( KMFNetZone* zone, const TQString& inOut ) {
	if ( zone->hosts().count() == 0 ) {
		return;
	}
	
	TQPtrListIterator<KMFTarget> it ( zone->hosts() );
	if ( inOut == "in" ) {
		m_compiledScript.append( "\n# Forbidden Clients\n" );
	} else {
		m_compiledScript.append( "\n# Forbidden Server\n" );
	}
	
	while ( it.current() ) {
		KMFNetHost *host = dynamic_cast<KMFNetHost*> ( *it );
		++it;
		if ( inOut == "in" ) {
			m_compiledScript.append( "block in quick from " );
			m_compiledScript.append( host->address()->toString() );
			m_compiledScript.append( " to any\n" );
		} else {
			m_compiledScript.append( "block out quick from any to " );
			m_compiledScript.append( host->address()->toString() );
			m_compiledScript.append( "\n" );
		}
	}
}

void KMFPFCompiler::setupICMPRules( KMFGenericDoc* doc ) {
	if ( ! doc->allowPingReply() ) {
		return;
	}
	m_compiledScript.append( "\n# Allow ping\n" );
	m_compiledScript.append( "pass in quick inet proto icmp all icmp-type { 0, 3, 11 } keep state\n" );
}

void KMFPFCompiler::setupLocalhostRules() {
	m_compiledScript.append( "\n# Allow loopback traffic\n" );
	m_compiledScript.append( "pass in quick on lo0 from 127.0.0.1 to 127.0.0.1 keep state\n" );
	m_compiledScript.append( "pass out quick on lo0 from 127.0.0.1 to 127.0.0.1 keep state\n" );
}

void KMFPFCompiler::createIncommingACCESSRules( KMFNetZone* zone, KMFGenericDoc* doc ) {
	kdDebug() << "void KMFPFCompiler::createIncommingACCESSRules( KMFNetZone* zone, KMFGenericDoc* doc )" << endl;
	if ( ! doc->allowIncomingConnections() ) {
		return;
	}
	if ( zone == 0 ) {
		m_compiledScript.append( TQString("\n# Settup Inncomming Connection access rules\n") );
		zone = doc->incomingZone();
	}
	
	TQPtrList<KMFNetZone>& children = zone->zones();
	TQPtrListIterator<KMFNetZone> it( children );
	while( it.current() ) {
		createIncommingACCESSRules(  it.current(), doc );
		++it;
	}
	
	TQPtrList<KMFTarget>& zoneHosts = zone->hosts();
	TQPtrListIterator<KMFTarget> itZoneHosts ( zoneHosts );
	while ( itZoneHosts.current() ) {
		KMFNetHost* host = dynamic_cast<KMFNetHost*> ( itZoneHosts.current() );
		++itZoneHosts;
		m_compiledScript.append( TQString("\n# Settup rules for host %1 [%2]\n").arg( host->guiName() ).arg( zone->address()->toString() ) );
		
		TQPtrList<KMFProtocolUsage>& hostProts = host->protocols();
		TQPtrListIterator<KMFProtocolUsage> itHostProtocols ( hostProts );
		while ( itHostProtocols.current() ) {
			KMFProtocolUsage* prot = itHostProtocols.current();
			++itHostProtocols;
			if ( ! host->protocolInherited( prot->protocol()->uuid() ) ) {
				m_compiledScript.append( TQString("# Settup rules for protocol %1\n").arg( prot->protocol()->name() ) );
				if ( prot->protocol()->tcpPorts().count() > 0 ) {
					m_compiledScript.append( "pass in " ); 
					if ( prot->logging() || host->logIncoming() ) {
						m_compiledScript.append( " log " );
					}
					m_compiledScript.append( " quick inet proto tcp from " );
					m_compiledScript.append( zone->address()->toString() );
					m_compiledScript.append( "/32 " );
					m_compiledScript.append( " to any " );
					m_compiledScript.append( " port " );
					m_compiledScript.append( getPortList( prot->protocol()->tcpPorts() ) );
					m_compiledScript.append( " synproxy state\n" );
				}
				if ( prot->protocol()->udpPorts().count() > 0 ) {
					m_compiledScript.append( "pass in " ); 
					if ( prot->logging() || host->logIncoming() ) {
						m_compiledScript.append( " log " );
					}
					m_compiledScript.append( " quick inet proto udp from " );
					m_compiledScript.append( zone->address()->toString() );
					m_compiledScript.append( "/32" );
					m_compiledScript.append( " to any " );
					m_compiledScript.append( " port " );
					m_compiledScript.append( getPortList( prot->protocol()->udpPorts() ) );
					m_compiledScript.append( " keep state\n" );
				}
			} else {
				kdDebug() << "Skipping inherited Portocol: " << prot->protocol()->name() << " in zone: " << zone->guiName() << endl;
			}
		}
	}
	
	m_compiledScript.append( TQString("\n# Settup rules for zone %1 [%2/%3]\n").arg( zone->guiName() ).arg( zone->address()->toString() ).arg( zone->maskLength() ) );
	TQPtrList<KMFProtocolUsage>& zoneProts = zone->protocols();
	TQPtrListIterator<KMFProtocolUsage> itZoneProtocols ( zoneProts );
	while ( itZoneProtocols.current() ) {
		KMFProtocolUsage* prot = itZoneProtocols.current();
		++itZoneProtocols;
		if ( ! zone->protocolInherited( prot->protocol()->uuid() ) ) {
			m_compiledScript.append( TQString("# Settup rules for protocol %1\n").arg( prot->protocol()->name() ) );
			if ( prot->protocol()->tcpPorts().count() > 0 ) {
				m_compiledScript.append( "pass in " ); 
				if ( prot->logging() ) {
					m_compiledScript.append( " log " );
				}
				m_compiledScript.append( " quick inet proto tcp from " );
				m_compiledScript.append( zone->address()->toString() );
				m_compiledScript.append( "/" );
				m_compiledScript.append( TQString::number( zone->maskLength() ) );
				m_compiledScript.append( " to any " );
				m_compiledScript.append( " port " );
				m_compiledScript.append( getPortList( prot->protocol()->tcpPorts() ) );
				m_compiledScript.append( " synproxy state\n" );
			}
			if ( prot->protocol()->udpPorts().count() > 0 ) {
				m_compiledScript.append( "pass in " ); 
				if ( prot->logging() ) {
					m_compiledScript.append( " log " );
				}
				m_compiledScript.append( " quick inet proto udp from " );
				m_compiledScript.append( zone->address()->toString() );
				m_compiledScript.append( "/" );
				m_compiledScript.append( TQString::number( zone->maskLength() ) );
				m_compiledScript.append( " to any " );
				m_compiledScript.append( " port " );
				m_compiledScript.append( getPortList( prot->protocol()->udpPorts() ) );
				m_compiledScript.append( " keep state\n" );
			}
		} else {
			kdDebug() << "Skipping inherited Portocol: " << prot->protocol()->name() << " in zone: " << zone->guiName() << endl;
		}
	}
}

void KMFPFCompiler::createOutgoingACCESSRules( KMFNetZone* zone, KMFGenericDoc* doc ) {
	kdDebug() << "void KMFPFCompiler::createOutgoingACCESSRules( KMFNetZone* zone, KMFGenericDoc* doc )" << endl;
	if ( ! doc->restrictOutgoingConnections() ) {
		return;
	}
	if ( zone == 0 ) {
		m_compiledScript.append( TQString("\n# Settup Outgoing Connection access rules\n") );
		zone = doc->outgoingZone();
	}
	
	TQPtrList<KMFNetZone>& children = zone->zones();
	TQPtrListIterator<KMFNetZone> it( children );
	while( it.current() ) {
		createIncommingACCESSRules(  it.current(), doc );
		++it;
	}
	
	TQPtrList<KMFTarget>& zoneHosts = zone->hosts();
	TQPtrListIterator<KMFTarget> itZoneHosts ( zoneHosts );
	while ( itZoneHosts.current() ) {
		KMFNetHost* host = dynamic_cast<KMFNetHost*> ( itZoneHosts.current() );
		++itZoneHosts;
		m_compiledScript.append( TQString("\n# Settup rules for host %1 [%2]\n").arg( host->guiName() ).arg( zone->address()->toString() ) );
		
		TQPtrList<KMFProtocolUsage>& hostProts = host->protocols();
		TQPtrListIterator<KMFProtocolUsage> itHostProtocols ( hostProts );
		while ( itHostProtocols.current() ) {
			KMFProtocolUsage* prot = itHostProtocols.current();
			++itHostProtocols;
			if ( ! host->protocolInherited( prot->protocol()->uuid() ) ) {
				m_compiledScript.append( TQString("# Settup rules for protocol %1\n").arg( prot->protocol()->name() ) );
				if ( prot->protocol()->tcpPorts().count() > 0 ) {
					m_compiledScript.append( "pass out " ); 
					if ( prot->logging() || host->logIncoming() ) {
						m_compiledScript.append( " log " );
					}
					m_compiledScript.append( " quick inet proto tcp from " );
					m_compiledScript.append( zone->address()->toString() );
					m_compiledScript.append( "/32 " );
					m_compiledScript.append( " to any " );
					m_compiledScript.append( " port " );
					m_compiledScript.append( getPortList( prot->protocol()->tcpPorts() ) );
					m_compiledScript.append( " synproxy state\n" );
				}
				if ( prot->protocol()->udpPorts().count() > 0 ) {
					m_compiledScript.append( "pass out " ); 
					if ( prot->logging() || host->logIncoming() ) {
						m_compiledScript.append( " log " );
					}
					m_compiledScript.append( " quick inet proto udp from " );
					m_compiledScript.append( zone->address()->toString() );
					m_compiledScript.append( "/32" );
					m_compiledScript.append( " to any " );
					m_compiledScript.append( " port " );
					m_compiledScript.append( getPortList( prot->protocol()->udpPorts() ) );
					m_compiledScript.append( " keep state\n" );
				}
			} else {
				kdDebug() << "Skipping inherited Portocol: " << prot->protocol()->name() << " in zone: " << zone->guiName() << endl;
			}
		}
	}
	
	m_compiledScript.append( TQString("\n# Settup rules for zone %1 [%2/%3]\n").arg( zone->guiName() ).arg( zone->address()->toString() ).arg( zone->maskLength() ) );
	TQPtrList<KMFProtocolUsage>& zoneProts = zone->protocols();
	TQPtrListIterator<KMFProtocolUsage> itZoneProtocols ( zoneProts );
	while ( itZoneProtocols.current() ) {
		KMFProtocolUsage* prot = itZoneProtocols.current();
		++itZoneProtocols;
		if ( ! zone->protocolInherited( prot->protocol()->uuid() ) ) {
			m_compiledScript.append( TQString("# Settup rules for protocol %1\n").arg( prot->protocol()->name() ) );
			if ( prot->protocol()->tcpPorts().count() > 0 ) {
				m_compiledScript.append( "pass out " ); 
				if ( prot->logging() ) {
					m_compiledScript.append( " log " );
				}
				m_compiledScript.append( " quick inet proto tcp from " );
				m_compiledScript.append( zone->address()->toString() );
				m_compiledScript.append( "/" );
				m_compiledScript.append( TQString::number( zone->maskLength() ) );
				m_compiledScript.append( " to any " );
				m_compiledScript.append( " port " );
				m_compiledScript.append( getPortList( prot->protocol()->tcpPorts() ) );
				m_compiledScript.append( " synproxy state\n" );
			}
			if ( prot->protocol()->udpPorts().count() > 0 ) {
				m_compiledScript.append( "pass out " ); 
				if ( prot->logging() ) {
					m_compiledScript.append( " log " );
				}
				m_compiledScript.append( " quick inet proto udp from " );
				m_compiledScript.append( zone->address()->toString() );
				m_compiledScript.append( "/" );
				m_compiledScript.append( TQString::number( zone->maskLength() ) );
				m_compiledScript.append( " to any " );
				m_compiledScript.append( " port " );
				m_compiledScript.append( getPortList( prot->protocol()->udpPorts() ) );
				m_compiledScript.append( " keep state\n" );
			}
		} else {
			kdDebug() << "Skipping inherited Portocol: " << prot->protocol()->name() << " in zone: " << zone->guiName() << endl;
		}
	}
}



TQString KMFPFCompiler::getPortList( TQValueList<int>& list ) {
	TQString ret = "{";
	TQValueList<int>::iterator ports;
	uint i = 0;
	for( ports = list.begin(); ports != list.end(); ++ports ) {
		ret.append( TQString::number( *ports ) );
		if ( i != list.count() - 1 ) {
			ret.append( ", " );
		}
		i++;
	}
	ret.append( "}" );
	return ret;
}



void KMFPFCompiler::setupPolicies( KMFGenericDoc* doc ) {
	m_compiledScript.append( "\n# Default action for unhandled packets\n" );
	m_compiledScript.append( "block in quick\n" );
	if ( doc->restrictOutgoingConnections() ) {
		m_compiledScript.append( "block out quick\n" );
	} else {
		m_compiledScript.append( "pass out quick all keep state\n" );
	}
}

void KMFPFCompiler::setupNatRules( KMFGenericDoc* doc ) {
	if ( ! doc->useNat() ) {
		return;
	}

	m_compiledScript.append( "\n# Setup NAT\n" );
	if ( doc->useMasquerade() ) {
		m_compiledScript.append( TQString("nat on %1 from any to any -> (%2)\n").arg( doc->outgoingInterface() ).arg( doc->outgoingInterface() ) );
	} else {
		m_compiledScript.append( TQString("nat on %1 from any to any -> %2\n").arg( doc->outgoingInterface() ).arg( doc->natAddress()->toString() ) );
	}
	

}

// TDEInstance* KMFPFCompilerFactory::s_instance = 0L;
// TDEAboutData* KMFPFCompilerFactory::s_about = 0L;

KMFPFCompilerFactory::KMFPFCompilerFactory( TQObject* parent, const char* name )
		: KLibFactory( parent, name ) {
// 	s_instance = new TDEInstance( "KMFPFCompilerFactory" );
}

TQObject* KMFPFCompilerFactory::createObject( TQObject* parent, const char* name,
        const char*, const TQStringList & ) {
	TQObject * obj = new KMFPFCompiler( parent, name );
	emit objectCreated( obj );
	return obj;
}


// TDEInstance* KMFPFCompilerFactory::instance() {
// 	if ( !s_instance ) {
// 		s_instance = new TDEInstance( "KMFPFCompilerFactory" );
// 	}
// 	return s_instance;
// }

extern "C" {
	void* init_libkmfcompiler_pf() {
		return new KMFPFCompilerFactory;
	}
}
}
#include "kmfpfcompiler.moc"