summaryrefslogtreecommitdiffstats
path: root/knetworkmanager-0.8/vpn-plugins/vpnc/src/knetworkmanager-vpnc.cpp
blob: a2c08e3236cd8caed0305c28111ac21326e625fa (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
/***************************************************************************
 *
 * knetworkmanager-vpnc.cpp - A NetworkManager frontend for KDE 
 *
 * Copyright (C) 2006 Novell, Inc.
 *
 * Author: Helmut Schaa <hschaa@suse.de>, <Helmut.Schaa@gmx.de>
 * Author: Timothy Pearson <kb9vqf@pearsoncomputing.net>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 **************************************************************************/

#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tqmessagebox.h>
#include <tqbutton.h>
#include <kcombobox.h>
#include <klineedit.h>
#include <kurlrequester.h>
#include <tqobjectlist.h>
#include <tqobject.h>
#include <tqcheckbox.h>
#include <kpassdlg.h>
#include <kgenericfactory.h>
#include <tqlabel.h>
#include <string>
#include <stdio.h>

#include "knetworkmanager-vpnc.h"
using namespace std;

typedef KGenericFactory<VPNCPlugin> VPNCPluginFactory;
K_EXPORT_COMPONENT_FACTORY( knetworkmanager_vpnc, VPNCPluginFactory("knetworkmanager_vpnc"));

#define NAT_MODE_CISCO 0
#define NAT_MODE_NATT 1
#define NAT_MODE_DISABLED 2

char linedata [2048];

VPNCPlugin::VPNCPlugin(TQObject* parent, const char* name, const TQStringList& args) 
	: VPNPlugin(parent, name, args)
{
	TDELocale* loc = TDEGlobal::locale();
	loc->insertCatalogue("NetworkManager-vpnc");
}

VPNCPlugin::~VPNCPlugin()
{

}

VPNConfigWidget* VPNCPlugin::CreateConfigWidget(TQWidget* parent)
{
	return new VPNCConfig(parent);
}

VPNAuthenticationWidget* VPNCPlugin::CreateAuthenticationWidget(TQWidget* parent)
{
	return new VPNCAuthentication(parent);
}


VPNCConfig::VPNCConfig(TQWidget* parent)
	: VPNConfigWidget(parent)
{
	TQVBoxLayout* layout = new TQVBoxLayout(this, 1, 1);
	_vpncWidget = new VPNCConfigWidget(this);
	layout->addWidget(_vpncWidget);

	connect(_vpncWidget->pcfImport, TQT_SIGNAL(clicked()), this, TQT_SLOT( pcfImport()) );	

	//connect(_vpncWidget->chkOverrideUsername, TQT_SIGNAL(toggled(bool)), _vpncWidget->Xauth_username, TQT_SLOT(setEnabled(bool)));
	//connect(_vpncWidget->chkOverrideUsername, TQT_SIGNAL(toggled(bool)), _vpncWidget->lblUsername, TQT_SLOT(setEnabled(bool)));

	connect(_vpncWidget->chkUseDomain, TQT_SIGNAL(toggled(bool)), _vpncWidget->Domain, TQT_SLOT(setEnabled(bool)));
	connect(_vpncWidget->chkUseDomain, TQT_SIGNAL(toggled(bool)), _vpncWidget->lblDomain, TQT_SLOT(setEnabled(bool)));

	connect(_vpncWidget->chkIPAdresses, TQT_SIGNAL(toggled(bool)), _vpncWidget->routes, TQT_SLOT(setEnabled(bool)));

	connect(_vpncWidget->chkUseKeepAlive, TQT_SIGNAL(toggled(bool)), _vpncWidget->nat_keep_alive_interval, TQT_SLOT(setEnabled(bool)));
	connect(_vpncWidget->chkUseKeepAlive, TQT_SIGNAL(toggled(bool)), _vpncWidget->lblKeepAliveInterval, TQT_SLOT(setEnabled(bool)));

	_vpncWidget->cboNAT->insertItem(i18n("Cisco UDP (default)"), NAT_MODE_CISCO);
	_vpncWidget->cboNAT->insertItem(i18n("NAT-T"), NAT_MODE_NATT);
	_vpncWidget->cboNAT->insertItem(i18n("Disabled"), NAT_MODE_DISABLED);

	this->languageChange();
}

VPNCConfig::~VPNCConfig()
{

}

void VPNCConfig::languageChange()
{

}

int VPNCConfig::getFileConfig(char *parameter, char *line) {
	int i;

	if (strstr(line, parameter) != NULL) {
		if ((strstr(line, parameter) - line) == 0) {
			for (i=0; i<(strlen(line)-strlen(parameter));i++) {
				linedata[i] = line[i+strlen(parameter)];
				if ((linedata[i] == 10) || (linedata[i] == 13)) {
					linedata[i] = 0;
				}
			}
			linedata[i-1]=0;

			return 0;
		}
		else {
			return 1;
		}
	}
	else {
		return 1;
	}
}

void VPNCConfig::pcfImport()
{
	const TQString& pcf_file = TQString(_vpncWidget->editPCF->url());
	printf("Asked for PCF import from file %s\n", pcf_file.ascii());
	int i;

	FILE *file = fopen ( pcf_file.ascii(), "r" );
	if ( file != NULL ) {
		char line [2048];	// or other suitable maximum line size
		// read a line
		while ( fgets ( line, sizeof line, file ) != NULL ) {
			// Parse the line and update global variables (current line in variable "line")
			if (getFileConfig("Host=", line) == 0) {
				//strdup(linedata)
				printf("Got configuration parameter Host with data %s\n", linedata);
				_vpncWidget->IPSec_gateway->setText(linedata);
			}
			if (getFileConfig("GroupName=", line) == 0) {
				printf("Got configuration parameter GroupName with data %s\n", linedata);
				_vpncWidget->IPSec_ID->setText(linedata);
			}
// 			if (getFileConfig("TcpTunnelingPort=", line) == 0) {
// 				printf("Got configuration parameter TcpTunnelingPort with data %s\n", linedata);
// 				_vpncWidget->IPSec_ID->setText(linedata);
// 			}
			if (getFileConfig("NTDomain=", line) == 0) {
				if (strlen(linedata) > 0) {
					printf("Got configuration parameter NTDomain with data %s\n", linedata);
					_vpncWidget->Domain->setText(linedata);
					_vpncWidget->chkUseDomain->setChecked(true);
				}
			}
			if (getFileConfig("GroupPwd=", line) == 0) {
				if (strlen(linedata) > 0) {
					printf("Got configuration parameter GroupPwd with data %s\n", linedata);
					KMessageBox::information(this, TQString("Your group password is: %1\n\r\n\rYou will need this information when you log on").arg(linedata));
				}
			}
			if (getFileConfig("enc_GroupPwd=", line) == 0) {
				if (strlen(linedata) > 0) {
					printf("Got configuration parameter enc_GroupPwd with data %s\n", linedata);

					// Decrypt the obfusticated password with /usr/lib/vpnc/cisco-decrypt
					string decryptcommand="/usr/lib/vpnc/cisco-decrypt ";
					FILE *pipe_decrypt;
					char decrypted_result[2048];
					int i;
			
					decryptcommand.append(linedata);
					printf("Group password decrypt command: %s\n", decryptcommand.c_str());
					if ((pipe_decrypt = popen(decryptcommand.c_str(), "r")) == NULL)
					{
						printf("Group password decrypt error\n");
					}
					else {
						fgets(decrypted_result, 2048, pipe_decrypt);
						pclose(pipe_decrypt);
						for (i=0;i<2048;i++) {
							if (decrypted_result[i] == 0) {
								decrypted_result[i-1]=0;
								i=2048;
							}
						}
						printf("Group password decrypt result: '%s'\n", decrypted_result);
					}
					KMessageBox::information(this, TQString("Your group password is: %1\n\r\n\rYou will need this information when you log on").arg(decrypted_result));
				}
			}
		}
		fclose ( file );
	}
	else
	{
		//printf("[WARN] Unable to open configuration file %s\n", pcf_file.ascii());
		KMessageBox::error(this, i18n("That configuration file does not exist!"));
	}
}

void VPNCConfig::setVPNData(const TQStringList& routes, const TQMap<TQString, TQString>& properties)
{
	// fill up our inputfields (only textfields atm)
	for(TQMap<TQString, TQString>::ConstIterator it = properties.begin(); it != properties.end(); ++it)
	{
		TQString entry = it.key();
		TQString value = it.data();

		if (entry == "IPSec gateway")
		{
			_vpncWidget->IPSec_gateway->setText(value);
		}
		else if (entry == "IPSec ID")
		{
			_vpncWidget->IPSec_ID->setText(value);
		}
		else if (entry == "Xauth username")
		{
			_vpncWidget->Xauth_username->setText(value);
			//_vpncWidget->chkOverrideUsername->setChecked(true);
		}
		else if (entry == "Domain")
		{
			_vpncWidget->Domain->setText(value);
			_vpncWidget->chkUseDomain->setChecked(true);
		}
		else if (entry == "NAT-Keepalive packet interval")
		{
			_vpncWidget->nat_keep_alive_interval->setText(value);
			_vpncWidget->chkUseKeepAlive->setChecked(true);
		}
		// backwards compatibility
		else if (entry == "Disable NAT Traversal")
		{
			_vpncWidget->cboNAT->setCurrentItem(NAT_MODE_DISABLED);
		}
		else if (entry == "NAT Traversal Mode")
		{
			if (value == "natt")
				_vpncWidget->cboNAT->setCurrentItem(NAT_MODE_NATT);
			else if (value == "cisco-udp")
				_vpncWidget->cboNAT->setCurrentItem(NAT_MODE_CISCO);
			else if (value == "none")
				_vpncWidget->cboNAT->setCurrentItem(NAT_MODE_DISABLED);
			else
			{
				// FIXME: unknown NAT mode
			}
		}
		else if (entry == "Enable Single DES")
		{
			_vpncWidget->chkSingleDES->setChecked(value == "yes" || value == "true");
		}
	}

	// set routes
	if (!routes.empty())
	{
		_vpncWidget->chkIPAdresses->setChecked(true);
		_vpncWidget->routes->setText(routes.join(" "));
	}
}

TQMap<TQString, TQString> VPNCConfig::getVPNProperties()
{
	// build a StingList of properties
	TQMap<TQString, TQString> strlist;
	strlist.insert("IPSec gateway", TQString(_vpncWidget->IPSec_gateway->text()));
	strlist.insert("IPSec ID", TQString(_vpncWidget->IPSec_ID->text()));

	//if (_vpncWidget->chkOverrideUsername->isChecked())
		strlist.insert("Xauth username", TQString(_vpncWidget->Xauth_username->text()));

	if (_vpncWidget->chkUseDomain->isChecked())
		strlist.insert("Domain", TQString(_vpncWidget->Domain->text()));
	
	if (_vpncWidget->chkUseKeepAlive->isChecked())
		strlist.insert("NAT-Keepalive packet interval", TQString(_vpncWidget->nat_keep_alive_interval->text()));

	switch (_vpncWidget->cboNAT->currentItem())
	{
		case NAT_MODE_CISCO:
			strlist.insert("NAT Traversal Mode", "cisco-udp");
			break;
		case NAT_MODE_NATT:
			strlist.insert("NAT Traversal Mode", "natt");
			break;
		case NAT_MODE_DISABLED:
		default:
			strlist.insert("NAT Traversal Mode", "none");
			break;
	}

	if (_vpncWidget->chkSingleDES->isChecked())
		strlist.insert("Enable Single DES", "yes");
	else
		strlist.insert("Enable Single DES", "no");

	return strlist;
}

TQStringList VPNCConfig::getVPNRoutes()
{
	TQStringList strlist;
	if(_vpncWidget->chkIPAdresses->isChecked())
	{
		strlist = TQStringList::split(" ", _vpncWidget->routes->text());
	}
	return strlist;

}

bool VPNCConfig::hasChanged()
{
	return true;
}

bool VPNCConfig::isValid(TQStringList& err_msg)
{
	bool retval = true;
	if(_vpncWidget->IPSec_gateway->text() == "" || _vpncWidget->IPSec_ID->text() == "")
	{
		err_msg.append(i18n("At least the gateway and group has to be supplied."));
		retval = false;
	}
	return retval;
}

VPNCAuthentication::VPNCAuthentication(TQWidget* parent, char* name)
	: VPNAuthenticationWidget(parent, name)
{
	TQVBoxLayout* layout = new TQVBoxLayout(this, 1, 1);
	_vpncAuth = new VPNCAuthenticationWidget(this);
	layout->addWidget(_vpncAuth);
}

VPNCAuthentication::~VPNCAuthentication()
{

}

TQMap<TQString, TQString> VPNCAuthentication::getPasswords()
{
	TQMap<TQString, TQString> pwds;
	pwds.insert("Xauth password", TQString(_vpncAuth->editUserPassword->password()));
	if (_vpncAuth->chkObfusticated->isChecked()) {
		// Decrypt the obfusticated password with /usr/lib/vpnc/cisco-decrypt
		string decryptcommand="/usr/lib/vpnc/cisco-decrypt ";
		FILE *pipe_decrypt;
		char decrypted_result[2048];
		int i;

		decryptcommand.append(_vpncAuth->editGroupPassword->password());
		printf("Group password decrypt command: %s\n", decryptcommand.c_str());
		if ((pipe_decrypt = popen(decryptcommand.c_str(), "r")) == NULL)
		{
			printf("Group password decrypt error\n");
		}
		else {
			fgets(decrypted_result, 2048, pipe_decrypt);
			pclose(pipe_decrypt);
			for (i=0;i<2048;i++) {
				if (decrypted_result[i] == 0) {
					decrypted_result[i-1]=0;
					i=2048;
				}
			}
			printf("Group password decrypt result: '%s'\n", decrypted_result);
			pwds.insert("IPSec secret", TQString(decrypted_result));
		}
	}
	else {
		pwds.insert("IPSec secret", TQString(_vpncAuth->editGroupPassword->password()));
	}
	return pwds;
}

void VPNCAuthentication::setPasswords(TQString name, TQString value) {
	if (name == TQString("Xauth password")) {
		_vpncAuth->editUserPassword->erase();
		_vpncAuth->editUserPassword->insert(value);
	}
	else if (name == TQString("IPSec secret")) {
		_vpncAuth->editGroupPassword->erase();
		_vpncAuth->editGroupPassword->insert(value);
	}
}