summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/ruleoptionplugins/nat_target_option/kmfruleeditortargetnat.cpp
blob: aae5ece851139c4546649967e93483dc29ce29f7 (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
/***************************************************************************
begin                : Sun May 26 2002
copyright            : (C) 2002 by Christian Hubinger
email                : chubinger@irrsinnig.org
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "kmfruleeditortargetnat.h"

// QT includes
#include <tqstring.h>
#include <tqspinbox.h>
#include <tqlineedit.h>
#include <tqcheckbox.h>
#include <tqmessagebox.h>

// KDE include
#include <kdebug.h>
#include <tdelocale.h>
#include <tdeapplication.h>
#include <tdemessagebox.h>

// MY includes
#include "../../core/xmlnames.h"
#include "../../core/iptrule.h"
#include "../../core/iptchain.h"
#include "../../core/iptable.h"
#include "../../core/iptruleoption.h"
#include "../../core/kmfdoc.h"
#include "../../core/kmfiptdoc.h"
#include "../../core/kmferror.h"
#include "../../core/kmfcheckinput.h"
#include "../../core/kmferrorhandler.h"
#include "../../core/kmfnetwork.h"
#include "../../core/kmfundoengine.h"
namespace KMF {
KMFRuleEditorTargetNat::KMFRuleEditorTargetNat( TQWidget *parent, const char *name, WFlags fl ) : KMyFirewallRuleEditorTargetNat( parent, name, fl ) {
	m_CheckInput = new KMFCheckInput();
	m_ErrorHandler = new KMFErrorHandler( "KMFRuleEditProtocol" );
	m_err = new KMFError() ;

}



KMFRuleEditorTargetNat::~KMFRuleEditorTargetNat() {}

void KMFRuleEditorTargetNat::loadRule( IPTRule *rule ) {
	c_use_ports->setChecked( false );
	c_use_port_range->setChecked( false );
	c_ip_range->setChecked( false );
	t_ip1->clear();
	t_ip2->clear();
	sb_port1->setValue(1);
	sb_port2->setValue(2);
	m_rule = rule;

	kdDebug() << "KMFRuleEditorTargetNat::loadRule(IPTRule *rule)" << endl;
	m_rule = rule;
	bool has_port = false;
	IPTRuleOption *opt = m_rule->getOptionForName( "tcp_opt" );
	if ( ! opt->isEmpty() )
		has_port = true;
	if ( ! has_port ) {
		opt = m_rule->getOptionForName( "udp_opt" );
		if ( ! opt->isEmpty() )
			has_port = true;
	}
	if ( ! has_port ) {
		opt = m_rule->getOptionForName( "tcp_multiport_opt" );
		if ( ! opt->isEmpty() )
			has_port = true;
	}
	if ( ! has_port ) {
		opt = m_rule->getOptionForName( "udp_multiport_opt" );
		if ( ! opt->isEmpty() )
			has_port = true;
	}
	c_use_ports->setEnabled( has_port );

	if ( mbsnat )
		opt = m_rule->getOptionForName( "target_snat_opt" );
	else
		opt = m_rule->getOptionForName( "target_dnat_opt" );

	TQStringList vals;
	vals = opt->getValues();
	TQString arg = "";
	arg = *vals.at( 0 );
	kdDebug() << "Found option String: " << arg << endl;
	if ( arg != XML::Undefined_Value && !arg.isEmpty() && arg != XML::BoolOff_Value ) {
		kdDebug() << "Found option String: " << arg << endl;
		TQString ips = "";
		TQString ports = "";
		TQString ip1 = "";
		TQString ip2 = "";
		TQString port1 = "";
		TQString port2 = "";
		if ( arg.contains( ":" ) && has_port ) {
			c_use_ports->setChecked( true );
			int del = arg.find( ':' );
			ips = arg.left( del );
			kdDebug() << "IP arg: " << ips << endl;
			ports = arg.right( arg.length() - del - 1 );
			kdDebug() << "Port arg: " << ports << endl;
			if ( ips.contains( "-" ) ) {
				c_ip_range->setChecked( true );
				int delim = ips.find( '-' );

				ip1 = ips.left( delim );
				kdDebug() << "IP arg1: " << ip1 << endl;
				t_ip1 ->setText( ip1 );

				ip2 = ips.right( ips.length() - delim - 1 );
				kdDebug() << "IP arg2: " << ip2 << endl;
				t_ip2 ->setText( ip2 );
			} else {
				c_ip_range->setChecked( false );
				t_ip1 ->setText( ips );
			}
			if ( ports.contains( "-" ) ) {
				c_use_port_range->setChecked( true );

				int delim = ports.find( '-' );
				port1 = ports.left( delim );
				kdDebug() << "Port arg1: " << port1 << endl;
				;
				int int_port1 = port1.toInt();
				sb_port1 ->setValue( int_port1 );

				port2 = ports.right( ports.length() - delim - 1 );
				kdDebug() << "Port arg2: " << port2 << endl;
				;
				int int_port2 = port2.toInt();
				sb_port2 ->setValue( int_port2 );

			} else {
				c_use_port_range->setChecked( false );
				kdDebug() << "Port arg: " << ports << endl;
				;
				int int_port1 = ports.toInt();
				sb_port1 ->setValue( int_port1 );
			}
		} else if ( arg.contains( "-" ) ) {
			c_use_ports->setChecked( false );
			c_ip_range->setChecked( true );
			int delim = arg.find( '-' );
			ip1 = arg.left( delim );
			kdDebug() << "IP arg1: " << ip1 << endl;
			t_ip1 ->setText( ip1 );
			ip2 = arg.right( arg.length() - delim - 1 );
			kdDebug() << "IP arg2: " << ip2 << endl;
			t_ip2 ->setText( ip2 );
		} else if ( ! arg.isEmpty() ){
			c_use_ports->setChecked( false );
			c_ip_range->setChecked( false );
			ip1 = arg;
			t_ip1 ->setText( ip1 );
		} else {
			c_use_ports->setChecked( false );
			c_ip_range->setChecked( false );
			t_ip1 ->clear();
			t_ip2 ->clear();
		}
	}
}


void KMFRuleEditorTargetNat::accept() {
	kdDebug() << "KMFRuleEditorTargetNat::accept()";
	KMFUndoEngine::instance()->startTransaction( 
		m_rule,
		i18n("Edit Rule: %1 Target NAT Option").arg( m_rule->name() ) 
	);

	bool ip_range = c_ip_range->isChecked();
	bool use_ports = c_use_ports->isChecked();
	bool use_port_range = c_use_port_range->isChecked();
	TQString ip1, ip2, port1, port2, arg_ip, arg_port;

	if ( ip_range ) {
		ip1 = t_ip1->text();
		ip2 = t_ip2->text();
		if ( ip1.isEmpty() ) {
			KMessageBox::sorry(this,i18n("You need as a minimum to define an IP address to make NAT working."),
												i18n("Argument not Valid"));
				KMFUndoEngine::instance()->abortTransaction();
				return;
		}
		if ( ip2.isEmpty() ) {
			KMessageBox::sorry(this,i18n("Please also set the upper boundary for your address range.."),
												i18n("Argument not Valid"));
				KMFUndoEngine::instance()->abortTransaction();
				return;
		}

		// Sanity checks +++++++++++++++++
		m_CheckInput->checkInput( ip1, "IP", m_err );
		if ( !m_ErrorHandler->showError( m_err ) ) {
			KMFUndoEngine::instance()->abortTransaction();
			return ;
		}
		m_CheckInput->checkInput( ip2, "IP", m_err );
		if ( !m_ErrorHandler->showError( m_err ) ) {
			KMFUndoEngine::instance()->abortTransaction();
			return ;
		}
		// ++++++++++++++++++++++++++++++++
		arg_ip = ip1 + "-" + ip2;
		kdDebug() << "IP Argument: " << arg_ip;
	} else {
		ip1 = t_ip1->text();
		// Sanity checks +++++++++++++++++
		m_CheckInput->checkInput( ip1, "IP", m_err );
		if ( !m_ErrorHandler->showError( m_err ) ) {
			KMFUndoEngine::instance()->abortTransaction();
			return ;
		}
		// ++++++++++++++++++++++++++++++++
		arg_ip = ip1;
		kdDebug() << "IP Argument: " << arg_ip;
	}

	if ( use_ports && c_use_ports->isEnabled() ) {
		if ( use_port_range ) {
			port1 = sb_port1->text();
			port2 = sb_port2->text();
			// Sanity checks +++++++++++++++++
			m_CheckInput->checkInput( port1, "PORT", m_err );
			if ( !m_ErrorHandler->showError( m_err ) ) {
				KMFUndoEngine::instance()->abortTransaction();
				return ;
			}
			m_CheckInput->checkInput( port2, "PORT", m_err );
			if ( !m_ErrorHandler->showError( m_err ) ) {
				KMFUndoEngine::instance()->abortTransaction();
				return ;
			}
			// ++++++++++++++++++++++++++++++++
			arg_port = port1 + "-" + port2;
			kdDebug() << "Port Argument: " << arg_port;
		} else {
			port1 = sb_port1->text();
			// Sanity checks +++++++++++++++++
			m_CheckInput->checkInput( port1, "PORT", m_err );
			if ( !m_ErrorHandler->showError( m_err ) ) {
				KMFUndoEngine::instance()->abortTransaction();
				return ;
			}
			// ++++++++++++++++++++++++++++++++

			arg_port = port1;
			kdDebug() << "Port Argument: " << arg_port;
		}
	}

	if ( mbsnat ) {
		TQPtrList<TQString>* options = new TQPtrList<TQString>;
		TQString* name = new TQString( "target_snat_opt" );
		TQString val = arg_ip;
		TQString arg = "";
		if ( !val.isEmpty() ) {
			arg.append( val );
		} else {
			KMessageBox::sorry( this, i18n( "Unable to work without values.\n"
			                             "Please specify at least an IP address." ) ,
										  i18n( "Nat Options" ) );
			KMFUndoEngine::instance()->abortTransaction();
			return ;
		}

		TQString val2 = arg_port;
		if ( !val2.isEmpty() ) {
			arg.append( ":" );
			arg.append( val2 );
		}
		options->append( new TQString( arg ) );
		// emit sigAddTargetOpt( name, options );
		m_rule->addTargetOption( *name, *options );

	} else {
		TQPtrList<TQString>* options = new TQPtrList<TQString>;
		TQString* name = new TQString( "target_dnat_opt" );
		TQString val = arg_ip;
		TQString arg = "";
		if ( !val.isEmpty() ) {
			arg.append( val );
		} else {
			KMessageBox::sorry( this, i18n( "Unable to work without values.\n"
			                             "Please specify at least an IP address." ) ,
										  i18n( "Nat Options" ) );
			KMFUndoEngine::instance()->abortTransaction();
			return ;
		}
		TQString val2 = arg_port;
		if ( !val2.isEmpty() ) {
			arg.append( ":" );
			arg.append( val2 );
		}
		options->append( new TQString( arg ) );
		m_rule->addTargetOption( *name, *options );
	}

	KMFUndoEngine::instance()->endTransaction();
	emit sigDocumentChanged();
}
void KMFRuleEditorTargetNat::slotHelp() {
	kdDebug() << "void KMFRuleEditorTargetNat::slotHelp()" << endl;
	kapp->invokeHelp( "targets" );
}
void KMFRuleEditorTargetNat::reject() {
	kdDebug() << "void KMFRuleEditorTargetNat::reject()" << endl;
	loadRule(m_rule);
	emit sigHideMe();
}

}

#include "kmfruleeditortargetnat.moc"