summaryrefslogtreecommitdiffstats
path: root/knetworkmanager-0.8/vpn-plugins/strongswan/src/knetworkmanager-strongswan.cpp
blob: 37cee816e820fa5b6b6fb03167aa7c4ef8383e24 (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
/***************************************************************************
 *
 * knetworkmanager-strongswan.cpp - A NetworkManager frontend for KDE
 *
 * Author: Thomas Kallenberg <tkallenb@hsr.ch>, <thomas@no-more-secrets.ch>
 *
 * Strongly based on the Code of Helmut Schaa <hschaa@suse.de>
 *
 * 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 <klocale.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 "knetworkmanager-strongswan.h"

typedef KGenericFactory<StrongswanPlugin> StrongswanPluginFactory;
K_EXPORT_COMPONENT_FACTORY( knetworkmanager_strongswan, StrongswanPluginFactory("knetworkmanager_strongswan"));


StrongswanPlugin::StrongswanPlugin(TQObject* parent, const char* name, const TQStringList& args)
	: VPNPlugin(parent, name, args)
{
	KLocale* loc = KGlobal::locale();
	loc->insertCatalogue("NetworkManager-strongswan");
}

StrongswanPlugin::~StrongswanPlugin()
{

}

VPNConfigWidget* StrongswanPlugin::CreateConfigWidget(TQWidget* parent)
{
	return new StrongswanConfig(parent);
}

VPNAuthenticationWidget* StrongswanPlugin::CreateAuthenticationWidget(TQWidget* parent)
{
	return new StrongswanAuthentication(parent);
}


StrongswanConfig::StrongswanConfig(TQWidget* parent)
	: VPNConfigWidget(parent)
{
	TQVBoxLayout* tqlayout = new TQVBoxLayout(this, 1, 1);
	_strongswanWidget = new StrongswanConfigWidget(this);
	tqlayout->addWidget(_strongswanWidget);

	/* TODO not sure if we need this here */
	this->languageChange();
}

StrongswanConfig::~StrongswanConfig()
{

}

void StrongswanConfig::languageChange()
{

}

	//	usercert agent password userkey
StrongswanConnectionType::CONNECTIONTYPE StrongswanConnectionType::mapString2ConnectionType(int prop)
{
    if (prop == 0)
        return psk;
    else if (prop == 1)
        return key;
    else if (prop == 2)
        return agent;
    return UNKNOWN;
}

int StrongswanConnectionType::mapConnectionType2String(CONNECTIONTYPE connType)
{
    switch(connType)
    {
        case psk:
            return 0;
        case key:
            return 1;
        case agent:
            return 2;
        default:
            return -1;
    }
    return -1;
}

void StrongswanConfig::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 == "gateway")
		{
			_strongswanWidget->gateway->setText(value);
		}
		else if (entry == "certificate")
		{
			_strongswanWidget->certificate->setURL(value);
		}
		else if (entry == "username")
		{
			_strongswanWidget->username->setText(value);
		}
		else if (entry == "method")
		{
            StrongswanConnectionType::CONNECTIONTYPE type = StrongswanConnectionType::mapString2ConnectionType(value.toInt());
			_strongswanWidget->authtype->setCurrentItem(type);
		}
		// Options
		else if (entry == "chkUDPenc")
		{
			_strongswanWidget->chkUDPenc->setChecked(value == "true");
		}
		else if (entry == "chkIPcomp")
		{
			_strongswanWidget->chkIPcomp->setChecked(value == "true");
		}
		else if (entry == "chkIPinner")
		{
			_strongswanWidget->chkIPinner->setChecked(value == "true");
		}
	}
}

TQMap<TQString, TQString> StrongswanConfig::getVPNProperties()
{
	// build a StingList of properties
	TQMap<TQString, TQString> strlist;

	strlist.insert("gateway", TQString(_strongswanWidget->gateway->text()));
	strlist.insert("certificate", TQString(_strongswanWidget->certificate->url()));
	strlist.insert("username", TQString(_strongswanWidget->username->text()));
	strlist.insert("method",
		TQString::number(StrongswanConnectionType::mapConnectionType2String((StrongswanConnectionType::CONNECTIONTYPE)_strongswanWidget->authtype->currentItem())));

	if (_strongswanWidget->chkUDPenc->isChecked())
		strlist.insert("encap", TQString("yes"));

	if (_strongswanWidget->chkIPcomp->isChecked())
		strlist.insert("ipcomp", TQString("yes"));

	if (_strongswanWidget->chkIPinner->isChecked())
		strlist.insert("virtual", TQString("yes"));

	return strlist;
}

TQStringList StrongswanConfig::getVPNRoutes()
{
	TQStringList strlist;
	/*if(_strongswanWidget->chkIPAdresses->isChecked())
	{
		strlist = TQStringList::split(" ", _strongswanWidget->routes->text());
	}
	*/
	return strlist;

}

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

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

StrongswanAuthentication::StrongswanAuthentication(TQWidget* parent, char* name)
	: VPNAuthenticationWidget(parent, name)
{
	TQVBoxLayout* tqlayout = new TQVBoxLayout(this, 1, 1);
	_strongswanAuth = new StrongswanAuthenticationWidget(this);
	tqlayout->addWidget(_strongswanAuth);
}

StrongswanAuthentication::~StrongswanAuthentication()
{

}

TQMap<TQString, TQString> StrongswanAuthentication::getPasswords()
{
	TQMap<TQString, TQString> pwds;
	pwds.insert("user", TQString(_strongswanAuth->username->text()));
	pwds.insert("password", TQString(_strongswanAuth->password->password()));
	return pwds;
}

void StrongswanAuthentication::setPasswords(TQString name, TQString value) {
	if (name == TQString("password")) {
		_strongswanAuth->password->erase();
		_strongswanAuth->password->insert(value);
	}
}