summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/compilers/iptables/kmfiptablescompiler.cpp
blob: c6315261113eaf8c70763902d8932b3aad1487ce (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
//
// C++ Implementation: kmfiptablescompiler
//
// Description:
//
//
// Author: Christian Hubinger <chubinger@irrsinnig.org>, (C) 2003
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "kmfiptablescompiler.h"

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

// KDE includes
#include <kdebug.h>
#include <tdelocale.h>
#include <kstandarddirs.h>
#include <tdefiledialog.h>
#include <tdetempfile.h>
#include <tdeio/netaccess.h>
#include <tdemessagebox.h>
#include <tdeapplication.h>

// Project includes
#include "../../version.h"
#include "../../core/xmlnames.h"
#include "../../core/kmfrulesetdoc.h"
#include "../../core/kmfgenericdoc.h"
#include "../../core/kmfiptdoc.h"
#include "../../core/kmferror.h"
#include "../../core/kmferrorhandler.h"
#include "../../core/kmfconfig.h"
#include "../../core/kmftarget.h"
#include "../../kmfwidgets/kmflistview.h"
#include "../../kmfwidgets/kmfselectactivetarget.h"

#include "kmfiptablesscriptgenerator.h"
#include "kmfiptablesdocumentconverter.h"

namespace KMF {
KMFIPTablesCompiler::KMFIPTablesCompiler( TQObject* parent, const char* name ) : KMFPlugin( parent, name )  {
	m_osName = "linux";
	m_osGUIName = "Linux";
	m_backendName = "iptables";
	m_backendGUIName = "IPTables";

	m_errorHandler = new KMFErrorHandler( "KMFIPTablesCompiler" );
	m_iptWidget = 0;
	new TDEAction( i18n( "&Export as IPTables (Linux) Script" ),  "fileexport",
	                             0, this, TQ_SLOT( slotExportIPT() ), actionCollection(), "compile_iptables" );
	
	if ( genericDoc() ) {
		new TDEAction( i18n( "&Convert to IPTables Document and View" ),  "fileexport",
		                                   0, this, TQ_SLOT( slotConvertToIPTDoc() ), actionCollection(), "convert_to_iptdoc" );

		setXMLFile( "kmfiptablescompiler.rc" );
		kdDebug() << "KMFIPTablesCompiler: Finished initialisation." << endl;
	}
}


KMFIPTablesCompiler::~KMFIPTablesCompiler() {}

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

void KMFIPTablesCompiler::compile() {
	kdDebug() << "void KMFIPTablesCompiler::compile()" << endl;
}

void KMFIPTablesCompiler::slotExportIPT() {
	kdDebug() << "KMFIPTablesCompiler::slotExportIPT()" << endl;
	
	KMFTarget* tg = KMFSelectActiveTarget::selectTarget( network(), i18n("<qt><p>Please select target from which the configuration should be exported as iptables script.</p></qt>") );
		
	if ( ! tg ) {
		return;
	}
	
	
	
	
	KURL url = KFileDialog::getSaveURL( ":", "*.sh|Shell Script (*.sh)" );
	TQString filename = url.fileName();
	if ( url.fileName().isEmpty() )
		return ;
	int answer = 0;
	while ( answer != 3 ) {
		if ( TDEIO::NetAccess::exists( url, false, TDEApplication::kApplication()->mainWidget() ) ) {
			if ( answer == 4 ) {
				slotExportIPT();
				return ;
			} else {
				answer = KMessageBox::warningYesNo( 0, i18n( "<qt>File <b>%1</b> already exists!</p>"
				                                    "<p><b>Overwrite the existing file?</b></p></qt>" ).arg( url.url() ) );
			}
		} else {
			answer = 3;
		}
	}
	TQString extension = filename.right( 3 );
	if ( extension != ".sh" )
		filename.append( ".sh" );
	url.setFileName( filename );
	KTempFile tempfile;
	
	
	m_err = tg->rulesetDoc()->createFirewallScript( tempfile.name() );
	
	
		
	if ( m_errorHandler->showError( m_err ) ) {
		if ( TDEIO::NetAccess::upload( tempfile.name(), url, TDEApplication::kApplication()->mainWidget() ) ) {
		//	statusBar() ->message( i18n( "Wrote file: " ) + url.fileName() , 5000 );
		} else {
			kdDebug() << "Couldn't upload file!!!" << tempfile.name() << endl;
			KMessageBox::detailedError( 0, i18n( "<qt><p>Saving file: <b>%1</b> Failed.</p></qt>" ).arg( url.url() ),
										i18n( "<qt><p>If you are working with remotely stored files "
												"make sure that the target host and the directory is reachable. "
												"</p></qt>" ) );
		}
	}
	
	tempfile.unlink();
}

// void KMFIPTablesCompiler::slotShowIPTScript() {
// 	//	TQString s = compile( genericDoc() );
// 	TQMultiLineEdit* ed = new TQMultiLineEdit( 0, "edit" );
// 	if ( genericDoc() ) {
// 		ed->setText( compile( genericDoc() ) );
// 	} else if ( iptablesDoc() ) {
// 		ed->setText( compile( iptablesDoc() ) );
// 	}
// 	setOutputWidget( ed );
// 	showOutput();
// }

const TQString& KMFIPTablesCompiler::compile( KMFGenericDoc* doc ) {
	kdDebug() << "const TQString& KMFIPTablesCompiler::compile( KMFGenericDoc* doc )" << endl;
	//kdDebug() << "Doc XLM:\n" << m_genericDoc->getXMLSniplet() << endl;
	KMFIPTablesDocumentConverter *converter = new KMFIPTablesDocumentConverter();
	m_iptdoc = converter->compileToIPTDoc( doc );
	delete converter;
	
	if ( m_iptdoc ) {
		TQString ret =  m_iptdoc->compile();
		m_iptdoc->deleteLater();
		return *( new TQString( ret ) );
	} else {
		return *( new TQString( "ERROR: Couldn't compile document - may be wrong type " ) );
	}
	
}

const TQString& KMFIPTablesCompiler::compile( KMFIPTDoc* doc ) {
	KMFIPTablesScriptGenerator *generator = new  KMFIPTablesScriptGenerator();
	TQString script = generator->compile( doc );
	delete generator;
	return *(new TQString( script ) );
}

void KMFIPTablesCompiler::slotConvertToIPTDoc() {
	if ( ! doc() ) {
		kdDebug() << "No document Available to compile" << endl;
		return;
	}
	KMFIPTablesDocumentConverter *converter = new KMFIPTablesDocumentConverter();
		
		
	m_iptdoc = converter->compileToIPTDoc( genericDoc() );
	delete converter;

	if ( ! m_iptWidget ) {
		m_iptWidget = new TQTabWidget( 0 , "TQTabWidget" );
		m_iptViewFilter = new KMFListView( 0, "view" );
		m_iptViewNat = new KMFListView( 0, "view" );
		m_iptViewMangle = new KMFListView( 0, "view" );
		m_iptWidget ->addTab( m_iptViewFilter, "Filter" );
		m_iptWidget ->addTab( m_iptViewNat, "Nat" );
		m_iptWidget ->addTab( m_iptViewMangle, "Mangle" );
	}
	m_iptViewFilter ->setEnabled( true );
	m_iptViewFilter->clear();
	m_iptViewFilter->slotLoadNode( m_iptdoc->table( Constants::FilterTable_Name ) );
	m_iptViewFilter->slotUpdateView();
	
	m_iptViewNat ->setEnabled( true );
	m_iptViewNat->clear();
	m_iptViewNat->slotLoadNode( m_iptdoc->table( Constants::NatTable_Name ) );
	m_iptViewNat->slotUpdateView();
	
	m_iptViewMangle ->setEnabled( true );
	m_iptViewMangle->clear();
	m_iptViewMangle->slotLoadNode( m_iptdoc->table( Constants::MangleTable_Name ) );
	m_iptViewMangle->slotUpdateView();

	m_iptWidget->setMinimumSize( 800, 600 );
	m_iptWidget->show();
	m_iptWidget->raise();
	
	m_iptdoc->deleteLater();
}

// It's usually safe to leave the factory code alone.. with the
// notable exception of the TDEAboutData data
#include <tdeaboutdata.h>
#include <tdelocale.h>

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

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

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


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

extern "C" {
	void* init_libkmfcompiler_ipt() {
		return new KMFIPTablesCompilerFactory;
	}
}
}
#include "kmfiptablescompiler.moc"