summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/installer/kmfinstallerplugin.cpp
blob: a6a69ee68f1dd95350ee03f2d636a6b7e220b07a (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
/***************************************************************************
*                                                                         *
*   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.                                   *
*                                                                         *
***************************************************************************/
/*
Author: Christian Hubinger <chubinger@irrsinnig.org>, (C) 2001
*/


#include "kmfinstallerplugin.h"


// QT includes
#include <tqfile.h>
#include <tqtextstream.h>
#include <tqmultilineedit.h>
#include <tqiconset.h>
#include <tqdatetime.h>

// KDE includes
#include <kinstance.h>
#include <kaction.h>
#include <kstdaction.h>
#include <kfiledialog.h>
#include <klocale.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <kmessagebox.h>
#include <ktrader.h>
#include <kstandarddirs.h>
#include <kio/netaccess.h>

// Project includes
#include "../core/kmfconfig.h"
#include "../core/kmfdoc.h"
#include "../core/kmfprotocol.h"
#include "../core/kmfrulesetdoc.h"
#include "../core/kmftarget.h"
#include "../core/kmftargetconfig.h"
#include "../core/kmfnetwork.h"
#include "../core/kmfiptdoc.h"
#include "../core/kmferror.h"
#include "../core/kmferrorhandler.h"
#include "../core/kmfgenericdoc.h"
#include "../core/kmfpluginfactory.h"
#include "../core/kmfcompilerinterface.h"
#include "../core/kmfinstallerinterface.h"
#include "../core/kprocesswrapper.h"
#include "../core/xmlnames.h"

#include "../kmfwidgets/kmfmainwindow.h"
#include "../kmfwidgets/kmfprocout.h"
#include "../kmfwidgets/kmfselectactivetarget.h"

#include "linux/kmfiptinstaller.h"

namespace KMF {

KMFInstallerPlugin::KMFInstallerPlugin( TQObject *parent, const char *name )
	: KMFPlugin( parent, name ) {

	m_docType = -1;
	KMFMainWindow* app = 0;
	app = dynamic_cast<KMFMainWindow*>( parent );
	if ( ! app ) {
		KMessageBox::error( 0, "Oops wrong parent class found for kmfinstallerplugin!!!" );
	}
	m_doc = app->network()->currentDoc();

// 	m_installerPlugin = 0;
	m_execWidget = 0;
	m_actionStopFw = new KAction( i18n( "&Reset IPTables" ), "decrypted",
	                              0, this, TQT_SLOT( slotStopFirewall() ), actionCollection(), "reset_iptables" );

	m_actionRunFw = new KAction( i18n( "Run Fir&ewall" ), "encrypted",
	                             0, this, TQT_SLOT( slotStartFirewall() ), actionCollection(), "run_firewall" );

	m_actionPreviewScript = new KAction( i18n( "&Preview Script" ), "mime_txt",
	                                     0 , this, TQT_SLOT( slotShowScript() ), actionCollection(), "preview_script" );

	m_generateInstallPackage  = new KAction( i18n( "&Generate Linux Installation Package" ), "fileexport",
			0 , this, TQT_SLOT( slotGenerateInstallerPackage() ), actionCollection(), "generate_install_package" );
	
	m_actionShowConfig = new KAction( i18n( "Show &All Tables" ), "messagebox_info",
	                                  0 , this, TQT_SLOT( slotShowConfig() ), actionCollection(), "show_ipt_config" );

	m_actionShowFilter = new KAction( i18n( "Show &Filter Table" ), "messagebox_info",
	                                  0 , this, TQT_SLOT( slotShowFilter() ), actionCollection(), "show_filter" );

	m_actionShowNat = new KAction( i18n( "Show &Nat Table" ), "messagebox_info",
	                               0 , this, TQT_SLOT( slotShowNat() ), actionCollection(), "show_nat" );

	m_actionShowMangle = new KAction( i18n( "Show &Mangle Table" ), "messagebox_info",
	                                  0 , this, TQT_SLOT( slotShowMangle() ), actionCollection(), "show_mangle" );

	m_actionMenu = new KActionMenu( i18n( "Show IPTables Configuration" ), "messagebox_info", this , "show_menu" );

	m_actionMenu->insert( m_actionShowConfig );
	m_actionMenu->insert( m_actionShowFilter );
	m_actionMenu->insert( m_actionShowNat );
	m_actionMenu->insert( m_actionShowMangle );
	m_actionMenu->setDelayed( false );
	m_actionMenu->setStickyMenu( true );
	actionCollection() ->insert( m_actionMenu );

	m_actionInstallFW = new KAction( i18n( "&Install Firewall" ), "down",
	                                 0 , this, TQT_SLOT( slotInstallFW() ), actionCollection(), "install_firewall" );

	m_actionUninstallFW = new KAction( i18n( "&Uninstall Firewall" ), "up",
	                                   0 , this, TQT_SLOT( slotUninstallFW() ), actionCollection(), "uninstall_firewall" );
	setXMLFile( "kmfinstallerpluginui.rc" );

	slotEnableActions( false );
	connect( app,TQT_SIGNAL( sigEnableActions( bool ) ),
         this, TQT_SLOT( slotEnableActions(bool ) ) );

	kdDebug() << "KMFInstallerPlugin: Finished Initialisationn " << endl;
}

KMFInstallerPlugin::~KMFInstallerPlugin() {}

bool KMFInstallerPlugin::isConfigValid() {
	bool configValid = false;
	if ( rulesetDoc() ) {
		configValid = rulesetDoc()->target()->config()->isValid();
		
		if ( ! configValid ) {
			if(  KMessageBox::questionYesNo ( 0, 
				i18n( "<qt><p>Do you want KMyFirewall to try to auto configure %1 settings</p></qt>" ).tqarg( rulesetDoc()->target()->toFriendlyString() ),
	                                        i18n( "Auto Configure Target" ), KStdGuiItem::yes(), KStdGuiItem::no() ) == KMessageBox::Yes ) {
				KMFError* err = rulesetDoc()->target()->tryAutoConfiguration();
				KMFErrorHandler* errH = new KMFErrorHandler( "Target Autoconfiguration" );
				errH->showError( err );
				
				if ( ! rulesetDoc()->target()->config()->isValid() ) {
					KMessageBox::error( KApplication::kApplication()->mainWidget(), i18n("<qt><p>Auto Confguration of: %1 could not determinate all needed setting. You'll need to configure the mmissing settings.</p></qt>").tqarg( rulesetDoc()->target()->toFriendlyString() ) );
				} else {
					KMessageBox::information( KApplication::kApplication()->mainWidget(), i18n("<qt><p>Auto Confguration of: %1 finished successfully.</p></qt>").tqarg( rulesetDoc()->target()->toFriendlyString() ) );
				}
				
				configValid = rulesetDoc()->target()->config()->isValid();
				delete errH;
				delete err;
			}
		}
	}
	return configValid;
}
KMFProcOut* KMFInstallerPlugin::execWidget() {
	if ( m_execWidget )
		return m_execWidget;
	kdDebug() << "Need to create new execWidget." << endl;
	m_execWidget = new KMFProcOut( (KMFMainWindow*) parent(), "KMFProcOut" );
	return m_execWidget;
}


void KMFInstallerPlugin::generateInstallerPackage( KMFTarget* tg ) {
	if ( ! isConfigValid() ) {
		const TQString & msg = i18n( "The curemnt target configurations seems to be invalid" );
		const TQString& cap = i18n("Invalid Configuration");
		KMessageBox::error( 0, msg, cap );
		return;
	}
	
	if ( rulesetDoc() ) {
		
	
		KMFInstallerInterface *inst2 = tg->installer(); 
		if ( ! inst2 )
			return;
		
		
		TQString remDir = rulesetDoc()->target()->getFishUrl();
		remDir.append( "/tmp/" );
		
		TQString path = remDir;
		path.append( "kmfpackage.kmfpkg" );
		KURL url( path );
		KURL remDirUrl( remDir );
		
		if ( url.fileName().isEmpty() )
			return;
		
		if ( KIO::NetAccess::exists( url, false, KApplication::kApplication()->mainWidget() ) ) {
			TQDateTime now = TQDateTime::tqcurrentDateTime();
			
			TQString backUp;
			backUp.append( url.url() );
			backUp.append( "_backup_" );
			backUp.append( now.toString( "dd.MM.yyyy.hh:mm:ss" ) );
			KURL newUrl( backUp );
			
			
			TQString backFileName = "kmfpackage.kmfpkg";
			backUp.append( "_backup_" );
			backUp.append( now.toString( "dd.MM.yyyy.hh:mm:ss" ) );
			
			
			KIO::NetAccess::file_move( url, newUrl, 700, true, true, KApplication::kApplication()->mainWidget() );
			KIO::NetAccess::fish_execute( remDir, "chmod 400 /tmp/" + backFileName, KApplication::kApplication()->mainWidget() );
		}
		
		inst2->generateInstallerPackage( tg, url );
		KIO::NetAccess::fish_execute( remDir, "chmod 700 /tmp/kmfpackage.kmfpkg", KApplication::kApplication()->mainWidget() );
	}
}



// ########## Slots for the Actions ###############
void KMFInstallerPlugin::slotShowScript() {
	kdDebug() << "void KMFInstallerPlugin::slotShowScript()" << endl;
	setOutputWidget( execWidget() );
	TQString script = rulesetDoc()->compile();
	kdDebug() << "\n\nCompiled Script: " << script << "\n\n" << endl;
	execWidget()->setText( script, i18n("Show firewall script...") );
	showOutput();
}

void KMFInstallerPlugin::slotShowConfig() {
	kdDebug() << "void KMFInstallerPlugin::slotShowConfig()" << endl;
	cmdShowRunningConfig( "all" );
}

void KMFInstallerPlugin::slotShowFilter() {
	kdDebug() << "void KMFInstallerPlugin::slotShowFilter()" << endl;
	cmdShowRunningConfig( Constants::FilterTable_Name );
}

void KMFInstallerPlugin::slotShowNat() {
	kdDebug() << "void KMFInstallerPlugin::slotShowNat()" << endl;
	cmdShowRunningConfig( Constants::NatTable_Name );
}

void KMFInstallerPlugin::slotShowMangle() {
	kdDebug() << "void KMFInstallerPlugin::slotShowMangle()" << endl;
	cmdShowRunningConfig( Constants::MangleTable_Name );
}

void KMFInstallerPlugin::cmdShowRunningConfig( const TQString& para ) {
	if ( ! isConfigValid() ) {
		const TQString & msg = i18n( "The curemnt target configurations seems to be invalid" );
		const TQString& cap = i18n("Invalid Configuration");
		KMessageBox::error( 0, msg, cap );
		return;
	}
	
	KMFInstallerInterface *inst2 = rulesetDoc()->target()->installer(); 
	if ( ! inst2 )
		return;
	
	inst2->cmdShowRunningConfig( para );
}


void KMFInstallerPlugin::slotStopFirewall() {
	kdDebug() << "void KMFInstallerPlugin::slotStopFirewall() " << endl;
	if ( ! isConfigValid() ) {
		const TQString & msg = i18n( "The curemnt target configurations seems to be invalid" );
		const TQString& cap = i18n("Invalid Configuration");
		KMessageBox::error( 0, msg, cap );
		return;
	}
	

	KMFInstallerInterface *inst2 =  rulesetDoc()->target()->installer(); 
	if ( ! inst2 ) {
		return;
	}
			
	inst2->cmdStopFW();
}

void KMFInstallerPlugin::slotStartFirewall() {
	kdDebug() << "void KMFInstallerPlugin::slotStartFirewall()" << endl;
	if ( ! isConfigValid() ) {
		const TQString & msg = i18n( "The curemnt target configurations seems to be invalid" );
		const TQString& cap = i18n("Invalid Configuration");
		KMessageBox::error( 0, msg, cap );
		return;
	}
	if ( rulesetDoc() ) {
		
		KMFInstallerInterface *inst2 =  rulesetDoc()->target()->installer(); 
		if ( ! inst2 ) {
			return;
		}
		inst2->cmdRunFW();
	}
}


void KMFInstallerPlugin::slotInstallFW() {
	kdDebug() << "void KMFInstallerPlugin::slotInstallFW()" << endl;
	if ( ! isConfigValid() ) {
		const TQString & msg = i18n( "The curemnt target configurations seems to be invalid" );
		const TQString& cap = i18n("Invalid Configuration");
		KMessageBox::error( 0, msg, cap );
		return;
	}
	
	
	KMFInstallerInterface *inst2 = rulesetDoc()->target()->installer();
	if ( ! inst2 ) {
		return;
	}
	inst2->cmdInstallFW();
}

void KMFInstallerPlugin::slotUninstallFW() {
	kdDebug() << "void KMFInstallerPlugin::slotUninstallFW()" << endl;
	if ( ! isConfigValid() ) {
		const TQString & msg = i18n( "The curemnt target configurations seems to be invalid" );
		const TQString& cap = i18n("Invalid Configuration");
		KMessageBox::error( 0, msg, cap );
		return;
	}

	KMFInstallerInterface *inst2 = rulesetDoc()->target()->installer(); 
	if ( ! inst2 ) {
		return;
	}
	inst2->cmdUninstallFW();
}

void KMFInstallerPlugin::slotGenerateInstallerPackage() {
	kdDebug() << "void KMFInstallerPlugin::slotGenerateInstallPackage()" << endl;
	if ( rulesetDoc() ) {
		KMFTarget* tg = KMFSelectActiveTarget::selectTarget( network(), i18n("<qt><p>Please select target from which the configuration should be exported into an installation package.</p></qt>") );
		
		if ( ! tg ) {
			return;
		}
		
		
	
		if ( ! tg->config()->isValid() ) {
			const TQString & msg = i18n( "The target configurations seems to be invalid" );
			const TQString& cap = i18n("Invalid Configuration");
			KMessageBox::error( 0, msg, cap );
			return;
		}
	
		KMFInstallerInterface *inst2 =  tg->installer(); 
		if ( ! inst2 )
			return;
		
		TQString msg = i18n( "<qt><p>KMyFirewall will create a shell script (*.sh) for you that includes a compressed archieve containing the scripts needed to install the firewall scripts into the boot system of the target computer<br>To install the package make it executabe <b>(e.g. chmod +x mypackage.kmfpkg</b> call the script without any parameter <b>(./mypackage.kmfpkg)</b><br>For other options (e.g. uninstall, extracting etc.) please call the script using the --help parameter <b>(e.g. ./mypackage.kmfpkg --help)</b> to display a detailed option list.</p></qt>" );
		TQString cap = i18n( "Generate Installation Package" );
		KMessageBox::information( 0, msg, cap, "generate_intsllation_package_howto", 0 );
		
		
		KURL url = KFileDialog::getSaveURL( ":", "*.kmfpkg|KMyFirewall Installer Package (*.kmfpkg)"  );

		
		
		
		
		if ( url.fileName().isEmpty() )
			return;
		
		int answer = 0;
		
		if ( KIO::NetAccess::exists( url, false, KApplication::kApplication()->mainWidget() ) ) {
			answer = KMessageBox::warningYesNo( 0, i18n( "<qt>File <b>%1</b> already exists!</p>"
					"<p><b>Overwrite the existing file?</b></p></qt>" ).tqarg( url.url() ) );
			if ( answer == KMessageBox::No ) {
					return;
			} 
		}
		inst2->generateInstallerPackage( tg, url );
		
		
		
		TQString remPath = url.protocol();
		if ( url.isLocalFile() ) {
			KProcess *proc = new KProcess();
			*proc << "chmod";
			*proc << "700" << url.path();
			proc->start( KProcess::Block );
			delete proc;
			proc = 0;
			kdDebug() << "Set perms for: " << url.path() << endl;
		}
	}

}


void KMFInstallerPlugin::slotEnableActions( bool ) {
// 	if ( on ) {
// 		m_actionStopFw->setEnabled( true );
// 		m_actionRunFw->setEnabled( true );
// 		m_actionPreviewScript->setEnabled( true );
// 		m_actionShowConfig->setEnabled( true );
// 		m_actionShowFilter->setEnabled( true );
// 		m_actionShowNat->setEnabled( true );
// 		m_actionShowMangle->setEnabled( true );
// 		m_actionMenu->setEnabled( true );
// 		m_actionInstallFW->setEnabled( true );
// 		m_actionUninstallFW->setEnabled( true );
// 		m_generateInstallPackage->setEnabled( true );
// 	} else {
// 		m_actionStopFw->setEnabled( false );
// 		m_actionRunFw->setEnabled( false );
// 		m_actionPreviewScript->setEnabled( false );
// /*		m_actionShowConfig->setEnabled( false );
// 		m_actionShowFilter->setEnabled( false );
// 		m_actionShowNat->setEnabled( false );
// 		m_actionShowMangle->setEnabled( false );
// 		m_actionMenu->setEnabled( false );*/
// 		m_actionInstallFW->setEnabled( false );
// 		m_actionUninstallFW->setEnabled( false );
// 		m_generateInstallPackage->setEnabled( false );
// 	}
}


// It's usually safe to leave the factory code alone.. with the
// notable exception of the KAboutData data
#include <kaboutdata.h>
#include <klocale.h>

KInstance* KMFInstallerPluginFactory::s_instance = 0L;

KMFInstallerPluginFactory::KMFInstallerPluginFactory( TQObject* parent, const char* name )
		: KLibFactory( parent, name ) {
	s_instance = new KInstance( "KMFInstallerPluginFactory" );
}



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


extern "C" {
	void* init_libkmfinstallerplugin() {
		return new KMFInstallerPluginFactory;
	}
}

}
#include "kmfinstallerplugin.moc"