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
|
/*
Copyright 2009 Will Stephenson <wstephenson@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "connectionpersistence.h"
#include <KConfigGroup>
#include <kwallet.h>
#include "connection.h"
#include "setting.h"
#include "settingpersistence.h"
#include "settings/802-11-wireless.h"
#include "settings/802-11-wirelesspersistence.h"
#include "settings/802-11-wireless-security.h"
#include "settings/802-11-wireless-securitypersistence.h"
#include "settings/802-1x.h"
#include "settings/802-1xpersistence.h"
#include "settings/802-3-ethernet.h"
#include "settings/802-3-ethernetpersistence.h"
#include "settings/cdma.h"
#include "settings/cdmapersistence.h"
#include "settings/gsm.h"
#include "settings/gsmpersistence.h"
#include "settings/ipv4.h"
#include "settings/ipv4persistence.h"
#include "settings/ppp.h"
#include "settings/ppppersistence.h"
#include "settings/pppoe.h"
#include "settings/pppoepersistence.h"
#include "settings/serial.h"
#include "settings/serialpersistence.h"
#include "settings/vpn.h"
#include "settings/vpnpersistence.h"
using namespace Knm;
const QString ConnectionPersistence::NETWORKMANAGEMENT_RCFILE = QLatin1String("networkmanagementrc");
const QString ConnectionPersistence::CONNECTION_PERSISTENCE_PATH = QLatin1String("networkmanagement/connections/");
QString ConnectionPersistence::s_walletFolderName = QLatin1String("Network Management");
WId ConnectionPersistence::s_walletWId = 0;
ConnectionPersistence::ConnectionPersistence(Connection * conn, KSharedConfig::Ptr config, SecretStorageMode mode)
: m_connection(conn), m_config(config), m_storageMode(mode)
{
}
ConnectionPersistence::ConnectionPersistence(KSharedConfig::Ptr config, SecretStorageMode mode)
: m_config(config), m_storageMode(mode)
{
KConfigGroup connection(config, "connection");
QString uuid = connection.readEntry("uuid");
QString type = connection.readEntry("type");
if (uuid.isEmpty() || type.isEmpty()) {
m_connection = 0;
} else {
m_connection = new Connection(QUuid(uuid), Connection::typeFromString(type));
kDebug() << m_connection->uuid();
}
}
ConnectionPersistence::~ConnectionPersistence()
{
qDeleteAll(m_persistences);
}
Connection * ConnectionPersistence::connection() const
{
return m_connection;
}
SettingPersistence * ConnectionPersistence::persistenceFor(Setting * setting)
{
SettingPersistence * sp = m_persistences.value(setting);
if (!sp)
switch (setting->type()) {
case Setting::Cdma:
sp = new CdmaPersistence(static_cast<CdmaSetting*>(setting), m_config, m_storageMode);
break;
case Setting::Gsm:
sp = new GsmPersistence(static_cast<GsmSetting*>(setting), m_config, m_storageMode);
break;
case Setting::Ipv4:
sp = new Ipv4Persistence(static_cast<Ipv4Setting*>(setting), m_config, m_storageMode);
break;
case Setting::Ppp:
sp = new PppPersistence(static_cast<PppSetting*>(setting), m_config, m_storageMode);
break;
case Setting::Pppoe:
sp = new PppoePersistence(static_cast<PppoeSetting*>(setting), m_config, m_storageMode);
break;
case Setting::Security8021x:
sp = new Security8021xPersistence(static_cast<Security8021xSetting*>(setting), m_config, m_storageMode);
break;
case Setting::Serial:
sp = new SerialPersistence(static_cast<SerialSetting*>(setting), m_config, m_storageMode);
break;
case Setting::Vpn:
sp = new VpnPersistence(static_cast<VpnSetting*>(setting), m_config, m_storageMode);
break;
case Setting::Wired:
sp = new WiredPersistence(static_cast<WiredSetting*>(setting), m_config, m_storageMode);
break;
case Setting::Wireless:
sp = new WirelessPersistence(static_cast<WirelessSetting*>(setting), m_config, m_storageMode);
break;
case Setting::WirelessSecurity:
sp = new WirelessSecurityPersistence(
static_cast<WirelessSecuritySetting*>(setting), m_config, m_storageMode
);
break;
}
if (sp) {
m_persistences.insert(setting, sp);
}
return sp;
}
void ConnectionPersistence::save()
{
// save connection settings
KConfigGroup cg(m_config, "connection");
cg.writeEntry("id", m_connection->name());
cg.writeEntry("uuid", m_connection->uuid().toString());
cg.writeEntry("type", Connection::typeAsString(m_connection->type()));
cg.writeEntry("autoconnect", m_connection->autoConnect());
if (m_connection->timestamp().isValid())
cg.writeEntry("timestamp", m_connection->timestamp());
cg.writeEntry("icon", m_connection->iconName());
// save each setting
foreach (Setting * setting, m_connection->settings()) {
SettingPersistence * sp = persistenceFor(setting);
sp->save();
}
m_config->sync();
// factor out to make a pure Qt version
bool readyForWalletWrite = false;
if (m_connection->hasSecrets() && m_storageMode == ConnectionPersistence::Secure) {
KWallet::Wallet * wallet = KWallet::Wallet::openWallet(KWallet::Wallet::LocalWallet(), walletWid(), KWallet::Wallet::Synchronous );
if( wallet && wallet->isOpen() ) {
if( !wallet->hasFolder( s_walletFolderName ) )
wallet->createFolder( s_walletFolderName );
if ( wallet->setFolder( s_walletFolderName ) ) {
readyForWalletWrite = true;
}
}
// could be merged with above loop for speed but this keeps the
// kde wallet dependencies in one place
if (readyForWalletWrite) {
foreach (Setting * setting, m_connection->settings()) {
SettingPersistence * sp = persistenceFor(setting);
QMap<QString,QString> secrets = sp->secrets();
if (!secrets.isEmpty()) {
wallet->writeMap(walletKeyFor(setting), secrets);
}
}
}
}
}
void ConnectionPersistence::load()
{
// load connection settings
KConfigGroup cg(m_config, "connection");
if (cg.exists()) { // don't bother to try if the KConfigGroup doesn't exist, save opening the wallet too
m_connection->setName(cg.readEntry("id"));
m_connection->setAutoConnect(cg.readEntry<bool>("autoconnect", false));
m_connection->setTimestamp(cg.readEntry<QDateTime>("timestamp", QDateTime()));
m_connection->setIconName(cg.readEntry("icon"));
// load each setting
foreach (Setting * setting, m_connection->settings()) {
SettingPersistence * sp = persistenceFor(setting);
sp->load();
}
}
}
QString ConnectionPersistence::walletKeyFor(const Setting * setting) const
{
return m_connection->uuid() + ';' + setting->name();
}
void ConnectionPersistence::loadSecrets()
{
KConfigGroup cg(m_config, "connection");
if (cg.exists()) {
bool haveResult = true;
EnumError::type errorCode = EnumError::NoError;
if (m_storageMode != ConnectionPersistence::Secure) {
foreach (Setting * setting, m_connection->settings()) {
setting->setSecretsAvailable(true);
}
} else if (!m_connection->hasSecrets() ||
m_connection->secretsAvailable()) {
} else if (KWallet::Wallet::isEnabled()) {
kDebug() << "opening wallet...";
KWallet::Wallet * wallet = KWallet::Wallet::openWallet(KWallet::Wallet::LocalWallet(),
walletWid(), KWallet::Wallet::Asynchronous);
if (wallet) {
haveResult = false;
disconnect(wallet, SIGNAL(walletOpened(bool)), this, 0);
connect(wallet, SIGNAL(walletOpened(bool)), this, SLOT(walletOpenedForRead(bool)));
} else {
errorCode = EnumError::WalletNotFound;
}
} else {
errorCode = EnumError::WalletDisabled;
}
if (haveResult) {
emit loadSecretsResult(errorCode);
}
}
return;
}
void ConnectionPersistence::walletOpenedForRead(bool success)
{
if (success) {
KWallet::Wallet * wallet = static_cast<KWallet::Wallet*>(sender());
if (wallet->isOpen() && wallet->hasFolder(s_walletFolderName) && wallet->setFolder(s_walletFolderName)) {
kDebug() << "Reading all entries for connection";
QMap<QString,QMap<QString,QString> > entries;
QString key = m_connection->uuid() + QLatin1String("*");
bool missingEntry = false;
if (wallet->readMapList(key, entries) == 0) {
foreach (Setting * setting, m_connection->settings()) {
QString settingKey = walletKeyFor(setting);
if (entries.contains(settingKey)) {
QMap<QString,QString> settingSecrets = entries.value(settingKey);
if (settingSecrets.isEmpty()) {
kDebug() << "no secrets found for" << settingKey;
missingEntry = true;
break;
}
// This line will dump the wep key or other valuable information to stderr,
// so do not commit it uncommented
//kDebug() << settingSecrets;
persistenceFor(setting)->restoreSecrets(settingSecrets);
} else if (setting->hasSecrets()) {
missingEntry = true;
}
}
kDebug() << "Check connection:";
kDebug() << "secretsAvailable:" << m_connection->secretsAvailable();
if (missingEntry)
emit loadSecretsResult(EnumError::MissingContents);
else
emit loadSecretsResult(EnumError::NoError);
} else {
kDebug() << "Wallet::readEntryList for :" << key << " failed";
emit loadSecretsResult(EnumError::MissingContents);
}
}
} else {
emit loadSecretsResult(EnumError::WalletOpenRefused);
}
}
// vim: sw=4 sts=4 et tw=100
|