diff --git a/libs/editor/settings/wireguardinterfacewidget.cpp b/libs/editor/settings/wireguardinterfacewidget.cpp index dc148ee0..4d5eeb3a 100644 --- a/libs/editor/settings/wireguardinterfacewidget.cpp +++ b/libs/editor/settings/wireguardinterfacewidget.cpp @@ -1,639 +1,642 @@ /* Copyright 2019 Bruce Anderson 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 . */ #include "debug.h" #include "wireguardinterfacewidget.h" #include "wireguardtabwidget.h" #include "ui_wireguardinterfacewidget.h" #include "uiutils.h" #include "simpleipv4addressvalidator.h" #include "simpleiplistvalidator.h" #include "wireguardkeyvalidator.h" +#include +#include +#include #include #include #include #include #include #include #include #include // Tags used in a WireGuard .conf file - Used for importing #define PNM_WG_CONF_TAG_INTERFACE "[Interface]" #define PNM_WG_CONF_TAG_ADDRESS "Address" #define PNM_WG_CONF_TAG_LISTEN_PORT "ListenPort" #define PNM_WG_CONF_TAG_MTU "MTU" #define PNM_WG_CONF_TAG_DNS "DNS" #define PNM_WG_CONF_TAG_FWMARK "FwMark" #define PNM_WG_CONF_TAG_PRIVATE_KEY "PrivateKey" #define PNM_WG_CONF_TAG_PEER "[Peer]" #define PNM_WG_CONF_TAG_PUBLIC_KEY "PublicKey" #define PNM_WG_CONF_TAG_PRESHARED_KEY "PresharedKey" #define PNM_WG_CONF_TAG_ALLOWED_IPS "AllowedIPs" #define PNM_WG_CONF_TAG_ENDPOINT "Endpoint" #define PNM_WG_CONF_TAG_PERSISTENT_KEEPALIVE "PersistentKeepalive" #define PNM_WG_CONF_TAG_TABLE "Table" #define PNM_WG_CONF_TAG_PRE_UP "PreUp" #define PNM_WG_CONF_TAG_POST_UP "PostUp" #define PNM_WG_CONF_TAG_PRE_DOWN "PreDown" #define PNM_WG_CONF_TAG_POST_DOWN "PostDown" #define PNM_WG_KEY_PEERS "peers" #define PNM_WG_KEY_MTU "mtu" #define PNM_WG_KEY_PEER_ROUTES "peer-routes" #define PNM_WG_PEER_KEY_ALLOWED_IPS "allowed-ips" #define PNM_WG_PEER_KEY_ENDPOINT "endpoint" #define PNM_WG_PEER_KEY_PERSISTENT_KEEPALIVE "persistent-keepalive" #define PNM_WG_PEER_KEY_PRESHARED_KEY "preshared-key" #define PNM_WG_PEER_KEY_PRESHARED_KEY_FLAGS "preshared-key-flags" #define PNM_WG_PEER_KEY_PUBLIC_KEY "public-key" // Keys for the NetworkManager configuration #define PNM_SETTING_WIREGUARD_SETTING_NAME "wireguard" #define PNM_WG_KEY_FWMARK "fwmark" #define PNM_WG_KEY_LISTEN_PORT "listen-port" #define PNM_WG_KEY_PRIVATE_KEY "private-key" #define PNM_WG_KEY_PRIVATE_KEY_FLAGS "private-key-flags" class WireGuardInterfaceWidget::Private { public: Private(); ~Private(); Ui_WireGuardInterfaceProp ui; NetworkManager::WireGuardSetting::Ptr setting; KSharedConfigPtr config; QPalette warningPalette; QPalette normalPalette; WireGuardKeyValidator *keyValidator; QRegularExpressionValidator *fwmarkValidator; QIntValidator *mtuValidator; QIntValidator *portValidator; bool privateKeyValid; bool fwmarkValid; bool listenPortValid; bool peersValid; NMVariantMapList peers; }; WireGuardInterfaceWidget::Private::Private(void) : privateKeyValid(false) , fwmarkValid(true) , listenPortValid(true) , peersValid(false) { } WireGuardInterfaceWidget::Private::~Private() { delete keyValidator; delete fwmarkValidator; delete mtuValidator; delete portValidator; } WireGuardInterfaceWidget::WireGuardInterfaceWidget(const NetworkManager::Setting::Ptr &setting, QWidget *parent, Qt::WindowFlags f) : SettingWidget(setting, parent, f) , d(new Private) { d->ui.setupUi(this); d->setting = setting.staticCast(); d->config = KSharedConfig::openConfig(); d->warningPalette = KColorScheme::createApplicationPalette(d->config); d->normalPalette = KColorScheme::createApplicationPalette(d->config); KColorScheme::adjustBackground(d->warningPalette, KColorScheme::NegativeBackground, QPalette::Base, KColorScheme::ColorSet::View, d->config); KColorScheme::adjustBackground(d->normalPalette, KColorScheme::NormalBackground, QPalette::Base, KColorScheme::ColorSet::View, d->config); connect(d->ui.privateKeyLineEdit, &PasswordField::textChanged, this, &WireGuardInterfaceWidget::checkPrivateKeyValid); connect(d->ui.privateKeyLineEdit, &PasswordField::passwordOptionChanged, this, &WireGuardInterfaceWidget::checkPrivateKeyValid); connect(d->ui.fwmarkLineEdit, &QLineEdit::textChanged, this, &WireGuardInterfaceWidget::checkFwmarkValid); connect(d->ui.listenPortLineEdit, &QLineEdit::textChanged, this, &WireGuardInterfaceWidget::checkListenPortValid); connect(d->ui.btnPeers, &QPushButton::clicked, this, &WireGuardInterfaceWidget::showPeers); d->ui.privateKeyLineEdit->setPasswordModeEnabled(true); d->ui.privateKeyLineEdit->setPasswordOptionsEnabled(true); d->ui.privateKeyLineEdit->setPasswordNotSavedEnabled(false); // This is done as a private variable rather than a local variable so it can be // used both here and to validate the private key later d->keyValidator = new WireGuardKeyValidator(this); // Create validator for listen port d->portValidator = new QIntValidator; d->portValidator->setBottom(0); d->portValidator->setTop(65535); // Create a validator for the fwmark input. Acceptable // inputs are: "off" and numbers in either decimal // or hex (with 0x prefix) d->fwmarkValidator = new QRegularExpressionValidator(QRegularExpression("(off)|([0-9]{0,10})|(0x[0-9a-fA-F]{1,8})")); d->ui.fwmarkLineEdit->setValidator(d->fwmarkValidator); // Create a validator for the MTU field. d->mtuValidator = new QIntValidator(); d->mtuValidator->setBottom(0); d->ui.mtuLineEdit->setValidator(d->mtuValidator); // Default Peer Routes to true d->ui.peerRouteCheckBox->setChecked(true); // Connect for setting check watchChangedSetting(); KAcceleratorManager::manage(this); if (setting && !setting->isNull()) { loadConfig(d->setting); } // Set the initial backgrounds on all the widgets checkPrivateKeyValid(); } WireGuardInterfaceWidget::~WireGuardInterfaceWidget() { delete d; } void WireGuardInterfaceWidget::loadConfig(const NetworkManager::Setting::Ptr &setting) { NetworkManager::WireGuardSetting::Ptr wireGuardSetting = setting.staticCast(); d->ui.privateKeyLineEdit->setText(wireGuardSetting->privateKey()); if (wireGuardSetting->listenPort() != 0) d->ui.listenPortLineEdit->setText(QString::number(wireGuardSetting->listenPort())); else d->ui.listenPortLineEdit->clear(); if (wireGuardSetting->fwmark() != 0) d->ui.fwmarkLineEdit->setText(QString::number(wireGuardSetting->fwmark())); else d->ui.fwmarkLineEdit->clear(); if (wireGuardSetting->mtu() != 0) d->ui.mtuLineEdit->setText(QString::number(wireGuardSetting->mtu())); else d->ui.mtuLineEdit->clear(); d->ui.peerRouteCheckBox->setChecked(wireGuardSetting->peerRoutes()); NetworkManager::Setting::SecretFlags type = wireGuardSetting->privateKeyFlags(); switch (type) { case NetworkManager::Setting::AgentOwned: d->ui.privateKeyLineEdit->setPasswordOption(PasswordField::StoreForUser); break; case NetworkManager::Setting::None: d->ui.privateKeyLineEdit->setPasswordOption(PasswordField::StoreForAllUsers); break; // Not saved is not a valid option for the private key so set it to StoreForUser instead case NetworkManager::Setting::NotSaved: d->ui.privateKeyLineEdit->setPasswordOption(PasswordField::StoreForUser); break; case NetworkManager::Setting::NotRequired: d->ui.privateKeyLineEdit->setPasswordOption(PasswordField::NotRequired); break; } d->peers = wireGuardSetting->peers(); loadSecrets(setting); } void WireGuardInterfaceWidget::loadSecrets(const NetworkManager::Setting::Ptr &setting) { NetworkManager::WireGuardSetting::Ptr wireGuardSetting = setting.staticCast(); if (wireGuardSetting) { const QString key = wireGuardSetting->privateKey(); if (!key.isEmpty()) d->ui.privateKeyLineEdit->setText(key); const NMVariantMapList peers = wireGuardSetting->peers(); if (!peers.isEmpty()) { // For each of the peers returned, see if it contains a preshared key for (QList::const_iterator i = peers.cbegin(); i != peers.cend(); i++) { if (i->contains(PNM_WG_PEER_KEY_PRESHARED_KEY)) { // We have a preshared key so find the matching public key in the local peer list QString currentPublicKey = (*i)[PNM_WG_PEER_KEY_PUBLIC_KEY].toString(); if (!currentPublicKey.isEmpty()) { for (QList::iterator j = d->peers.begin(); j != d->peers.end(); j++) { if ((*j)[PNM_WG_PEER_KEY_PUBLIC_KEY].toString() == currentPublicKey) { (*j)[PNM_WG_PEER_KEY_PRESHARED_KEY] = (*i)[PNM_WG_PEER_KEY_PRESHARED_KEY].toString(); break; } } } } } } } // On the assumption that a saved configuration is valid (because how could it be saved if it // wasn't valid) then the peers section is valid unless it didn't get a preshared key if one // is required, so a simple minded "validity" check is done here. Real validity checks are done // when the peers widget is open which is the only time changes which might be invalid are // possible. d->peersValid = true; for (QList::iterator j = d->peers.begin(); j != d->peers.end(); j++) { if ((*j).contains(PNM_WG_PEER_KEY_PRESHARED_KEY_FLAGS) && (*j)[PNM_WG_PEER_KEY_PRESHARED_KEY_FLAGS] != NetworkManager::Setting::NotRequired && (!(*j).contains(PNM_WG_PEER_KEY_PRESHARED_KEY) || (*j)[PNM_WG_PEER_KEY_PRESHARED_KEY].toString().isEmpty())) { d->peersValid = false; break; } } } QVariantMap WireGuardInterfaceWidget::setting() const { NetworkManager::WireGuardSetting wgSetting; QString val = d->ui.fwmarkLineEdit->displayText(); if (!val.isEmpty()) wgSetting.setFwmark(val.toUInt()); val = d->ui.listenPortLineEdit->displayText(); if (!val.isEmpty()) wgSetting.setListenPort(val.toUInt()); val = d->ui.mtuLineEdit->displayText(); if (!val.isEmpty()) wgSetting.setMtu(val.toUInt()); val = d->ui.privateKeyLineEdit->text(); if (!val.isEmpty()) wgSetting.setPrivateKey(val); wgSetting.setPeerRoutes(d->ui.peerRouteCheckBox->isChecked()); PasswordField::PasswordOption option = d->ui.privateKeyLineEdit->passwordOption(); switch (option) { case PasswordField::StoreForUser: wgSetting.setPrivateKeyFlags(NetworkManager::Setting::AgentOwned); break; case PasswordField::StoreForAllUsers: wgSetting.setPrivateKeyFlags(NetworkManager::Setting::None); break; // Always Ask is not a valid option for the private key so set it to AgentOwned instead case PasswordField::AlwaysAsk: wgSetting.setPrivateKeyFlags(NetworkManager::Setting::AgentOwned); break; case PasswordField::NotRequired: wgSetting.setPrivateKeyFlags(NetworkManager::Setting::NotRequired); break; } wgSetting.setPeers(d->peers); return wgSetting.toMap(); } bool WireGuardInterfaceWidget::isValid() const { return d->privateKeyValid && d->fwmarkValid && d->listenPortValid && d->peersValid; } void WireGuardInterfaceWidget::checkPrivateKeyValid() { int pos = 0; PasswordField *widget = d->ui.privateKeyLineEdit; QString value = widget->text(); bool valid = (QValidator::Acceptable == d->keyValidator->validate(value, pos)); d->privateKeyValid = valid; setBackground(widget, valid); slotWidgetChanged(); } void WireGuardInterfaceWidget::checkFwmarkValid() { int pos = 0; QLineEdit *widget = d->ui.fwmarkLineEdit; QString value = widget->displayText(); d->fwmarkValid = QValidator::Acceptable == widget->validator()->validate(value, pos) || value.isEmpty(); setBackground(widget, d->fwmarkValid); slotWidgetChanged(); } void WireGuardInterfaceWidget::checkListenPortValid() { int pos = 0; QLineEdit *widget = d->ui.listenPortLineEdit; QString value = widget->displayText(); d->listenPortValid = QValidator::Acceptable == d->portValidator->validate(value, pos) || value.isEmpty(); setBackground(widget, d->listenPortValid); slotWidgetChanged(); } void WireGuardInterfaceWidget::setBackground(QWidget *w, bool result) const { if (result) w->setPalette(d->normalPalette); else w->setPalette(d->warningPalette); } QString WireGuardInterfaceWidget::supportedFileExtensions() { return "*.conf"; } void WireGuardInterfaceWidget::showPeers() { QPointer peers = new WireGuardTabWidget(d->peers, this); connect(peers.data(), &WireGuardTabWidget::accepted, [peers, this] () { NMVariantMapList peersData = peers->setting(); if (!peersData.isEmpty()) { d->peers = peersData; // The peers widget won't allow "OK" to be hit // unless all the peers are valid so no need to check d->peersValid = true; slotWidgetChanged(); } }); connect(peers.data(), &WireGuardTabWidget::finished, [peers] () { if (peers) { peers->deleteLater(); } }); peers->setModal(true); peers->show(); } NMVariantMapMap WireGuardInterfaceWidget::importConnectionSettings(const QString &fileName) { NMVariantMapMap result; QFile impFile(fileName); QList ipv4AddressList; QList ipv6AddressList; if (!impFile.open(QFile::ReadOnly|QFile::Text)) { return result; } const QString connectionName = QFileInfo(fileName).completeBaseName(); NMStringMap dataMap; NMVariantMapList peers; QVariantMap ipv4Data; QVariantMap *currentPeer = nullptr; WireGuardKeyValidator keyValidator; NetworkManager::Ipv4Setting ipv4Setting; NetworkManager::Ipv6Setting ipv6Setting; NetworkManager::WireGuardSetting wgSetting; QString proxyType; QString proxyUser; QString proxyPasswd; bool havePrivateKey = false; bool haveIpv4Setting = false; bool haveIpv6Setting = false; // Set the "PEER" elements true because they // need to be true to allocate the // first [Peer] section below bool havePublicKey = true; bool haveAllowedIps = true; int pos = 0; QTextStream in(&impFile); enum {IDLE, INTERFACE_SECTION, PEER_SECTION} currentState = IDLE; ipv4Setting.setMethod(NetworkManager::Ipv4Setting::Disabled); ipv6Setting.setMethod(NetworkManager::Ipv6Setting::Ignored); while (!in.atEnd()) { QStringList keyValue; QString line = in.readLine(); // Remove comments starting with '#' if (int index = line.indexOf('#') > 0) line.truncate(index); // Ignore blank lines if (line.isEmpty()) continue; keyValue.clear(); keyValue << line.split('='); if (keyValue[0] == PNM_WG_CONF_TAG_INTERFACE) { currentState = INTERFACE_SECTION; continue; } else if (keyValue[0] == PNM_WG_CONF_TAG_PEER) { // Check to make sure the previous PEER section has // all the required elements. If not it's an error // so just return the empty result. if (!havePublicKey || !haveAllowedIps) { return result; } else { havePublicKey = false; haveAllowedIps = false; currentState = PEER_SECTION; peers.append(*(new QVariantMap)); currentPeer = &peers[peers.size() - 1]; continue; } } // If we didn't get an '=' sign in the line, it's probably an error but // we're going to treat it as a comment and ignore it if (keyValue.length() < 2) continue; QString key = keyValue[0].trimmed(); // If we are in the [Interface] section look for the possible tags if (currentState == INTERFACE_SECTION) { // Address if (key == PNM_WG_CONF_TAG_ADDRESS) { QStringList valueList = keyValue[1].split(','); if (valueList.isEmpty()) return result; for (const QString &address : valueList) { const QPair addressIn = QHostAddress::parseSubnet(address.trimmed()); NetworkManager::IpAddress *addr = new NetworkManager::IpAddress; addr->setIp(addressIn.first); addr->setPrefixLength(addressIn.second); if (addressIn.first.protocol() == QAbstractSocket::NetworkLayerProtocol::IPv4Protocol) { ipv4AddressList.append(*addr); } else if (addressIn.first.protocol() == QAbstractSocket::NetworkLayerProtocol::IPv6Protocol) { ipv6AddressList.append(*addr); } else { // Error condition return result; } } if (!ipv4AddressList.isEmpty()) { ipv4Setting.setAddresses(ipv4AddressList); ipv4Setting.setMethod(NetworkManager::Ipv4Setting::Manual); haveIpv4Setting = true; } if (!ipv6AddressList.isEmpty()) { ipv6Setting.setAddresses(ipv6AddressList); ipv6Setting.setMethod(NetworkManager::Ipv6Setting::Manual); haveIpv6Setting = true; } } // Listen Port else if (key == PNM_WG_CONF_TAG_LISTEN_PORT) { uint val = keyValue[1].toUInt(); if (val <= 65535) wgSetting.setListenPort(val); } else if (key == PNM_WG_CONF_TAG_PRIVATE_KEY) { QString val = keyValue[1].trimmed(); // WireGuard keys present a slight problem because they // must end in '=' which is removed during the 'split' above // so if there are 3 parts and the 3 is empty, there must // have been an '=' so add it back on if (keyValue.size() == 3 && keyValue[2].isEmpty()) val += "="; if (QValidator::Acceptable == keyValidator.validate(val, pos)) { wgSetting.setPrivateKey(val); havePrivateKey = true; } } else if (key == PNM_WG_CONF_TAG_DNS) { QStringList addressList = keyValue[1].split(','); QList ipv4DnsList; QList ipv6DnsList; if (!addressList.isEmpty()) { for (const QString &address : addressList) { const QPair addressIn = QHostAddress::parseSubnet(address.trimmed()); if (addressIn.first.protocol() == QAbstractSocket::NetworkLayerProtocol::IPv4Protocol) { ipv4DnsList.append(addressIn.first); } else if (addressIn.first.protocol() == QAbstractSocket::NetworkLayerProtocol::IPv6Protocol) { ipv6DnsList.append(addressIn.first); } else { // Error condition return result; } } } // If there are any addresses put them on the correct tab and set the "method" to // "Automatic (Only addresses)" by setting "ignore-auto-dns=true" if (!ipv4DnsList.isEmpty()) { if (ipv4Setting.method() == NetworkManager::Ipv4Setting::Disabled) ipv4Setting.setMethod(NetworkManager::Ipv4Setting::Automatic); ipv4Setting.setIgnoreAutoDns(true); ipv4Setting.setDns(ipv4DnsList); haveIpv4Setting = true; } if (!ipv6DnsList.isEmpty()) { ipv6Setting.setMethod(NetworkManager::Ipv6Setting::Automatic); ipv6Setting.setIgnoreAutoDns(true); ipv6Setting.setDns(ipv6DnsList); haveIpv6Setting = true; } } else if (key == PNM_WG_CONF_TAG_MTU) { uint val = keyValue[1].toUInt(); if (val > 0) wgSetting.setMtu(val); } else if (key == PNM_WG_CONF_TAG_FWMARK) { uint val; if (keyValue[1].trimmed().toLower() == QLatin1String("off")) val = 0; else val = keyValue[1].toUInt(); wgSetting.setFwmark(val); } else if (key == PNM_WG_CONF_TAG_TABLE || key == PNM_WG_CONF_TAG_PRE_UP || key == PNM_WG_CONF_TAG_POST_UP || key == PNM_WG_CONF_TAG_PRE_DOWN || key == PNM_WG_CONF_TAG_POST_DOWN) { // plasma-nm does not handle these items } else { // We got a wrong field in the Interface section so it // is an error break; } } else if (currentState == PEER_SECTION) { // Public Key if (key == PNM_WG_CONF_TAG_PUBLIC_KEY) { QString val = keyValue[1].trimmed(); // WireGuard keys present a slight problem because they // must end in '=' which is removed during the 'split' above // so if there are 3 parts and the 3 is empty, there must // have been an '=' so add it back on if (keyValue.size() == 3 && keyValue[2].isEmpty()) val += "="; if (QValidator::Acceptable == keyValidator.validate(val, pos)) { currentPeer->insert(PNM_WG_PEER_KEY_PUBLIC_KEY, val); havePublicKey = true; } } else if (key == PNM_WG_CONF_TAG_ALLOWED_IPS) { SimpleIpListValidator validator(SimpleIpListValidator::WithCidr, SimpleIpListValidator::Both); QString val = keyValue[1].trimmed(); if (QValidator::Acceptable == validator.validate(val, pos)) { QStringList valList = val.split(','); for (QString& str : valList) str = str.trimmed(); currentPeer->insert(PNM_WG_PEER_KEY_ALLOWED_IPS, valList); haveAllowedIps = true; } } else if (key == PNM_WG_CONF_TAG_ENDPOINT) { if (keyValue[1].length() > 0) currentPeer->insert(PNM_WG_PEER_KEY_ENDPOINT, keyValue[1].trimmed()); } else if (key == PNM_WG_CONF_TAG_PRESHARED_KEY) { QString val = keyValue[1].trimmed(); // WireGuard keys present a slight problem because they // must end in '=' which is removed during the 'split' above // so if there are 3 parts and the 3 is empty, there must // have been an '=' so add it back on if (keyValue.size() == 3 && keyValue[2].isEmpty()) val += "="; if (QValidator::Acceptable == keyValidator.validate(val, pos)) { currentPeer->insert(PNM_WG_PEER_KEY_PRESHARED_KEY, val); } } } else { return result; } } if (!havePrivateKey || !haveAllowedIps || !havePublicKey || !haveAllowedIps) return result; QVariantMap conn; wgSetting.setPeers(peers); conn.insert("id", connectionName); conn.insert("interface-name", connectionName); conn.insert("type", "wireguard"); conn.insert("autoconnect", "false"); result.insert("connection", conn); result.insert("wireguard", wgSetting.toMap()); if (haveIpv4Setting) result.insert("ipv4", ipv4Setting.toMap()); if (haveIpv6Setting) result.insert("ipv6", ipv6Setting.toMap()); impFile.close(); return result; } diff --git a/libs/editor/settings/wireguardpeerwidget.cpp b/libs/editor/settings/wireguardpeerwidget.cpp index ee45bfcd..37ab04b1 100644 --- a/libs/editor/settings/wireguardpeerwidget.cpp +++ b/libs/editor/settings/wireguardpeerwidget.cpp @@ -1,374 +1,375 @@ /* Copyright 2019 Bruce Anderson 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 . */ #include "debug.h" #include "wireguardpeerwidget.h" #include "wireguardtabwidget.h" #include "ui_wireguardpeerwidget.h" #include "uiutils.h" #include "simpleipv4addressvalidator.h" #include "simpleiplistvalidator.h" #include "wireguardkeyvalidator.h" #include +#include #include #include #include #include #include #include #include // Keys for the NetworkManager configuration #define PNM_SETTING_WIREGUARD_SETTING_NAME "wireguard" #define PNM_WG_KEY_PEERS "peers" #define PNM_WG_KEY_MTU "mtu" #define PNM_WG_KEY_PEER_ROUTES "peer-routes" #define PNM_WG_PEER_KEY_ALLOWED_IPS "allowed-ips" #define PNM_WG_PEER_KEY_ENDPOINT "endpoint" #define PNM_WG_PEER_KEY_PERSISTENT_KEEPALIVE "persistent-keepalive" #define PNM_WG_PEER_KEY_PRESHARED_KEY "preshared-key" #define PNM_WG_PEER_KEY_PRESHARED_KEY_FLAGS "preshared-key-flags" #define PNM_WG_PEER_KEY_PUBLIC_KEY "public-key" static WireGuardKeyValidator keyValidator; static SimpleIpListValidator allowedIPsValidator(SimpleIpListValidator::WithCidr, SimpleIpListValidator::Both); class WireGuardPeerWidget::Private { public: Private(); ~Private(); Ui_WireGuardPeersProp ui; NetworkManager::WireGuardSetting::Ptr setting; KSharedConfigPtr config; QPalette warningPalette; QPalette normalPalette; QVariantMap peerData; bool publicKeyValid; bool allowedIPsValid; bool endpointValid; bool presharedKeyValid; }; WireGuardPeerWidget::Private::Private(void) : publicKeyValid(false) , allowedIPsValid(false) , endpointValid(true) , presharedKeyValid(true) { } WireGuardPeerWidget::Private::~Private() { } WireGuardPeerWidget::WireGuardPeerWidget(const QVariantMap &peerData, QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f) , d(new Private) { d->ui.setupUi(this); d->peerData = peerData; d->config = KSharedConfig::openConfig(); d->warningPalette = KColorScheme::createApplicationPalette(d->config); d->normalPalette = KColorScheme::createApplicationPalette(d->config); KColorScheme::adjustBackground(d->warningPalette, KColorScheme::NegativeBackground, QPalette::Base, KColorScheme::ColorSet::View, d->config); KColorScheme::adjustBackground(d->normalPalette, KColorScheme::NormalBackground, QPalette::Base, KColorScheme::ColorSet::View, d->config); setWindowTitle(i18nc("@title: window wireguard peers properties", "WireGuard peers properties")); connect(d->ui.publicKeyLineEdit, &QLineEdit::textChanged, this, &WireGuardPeerWidget::checkPublicKeyValid); connect(d->ui.allowedIPsLineEdit, &QLineEdit::textChanged, this, &WireGuardPeerWidget::checkAllowedIpsValid); connect(d->ui.endpointAddressLineEdit, &QLineEdit::textChanged, this, &WireGuardPeerWidget::checkEndpointValid); connect(d->ui.endpointPortLineEdit, &QLineEdit::textChanged, this, &WireGuardPeerWidget::checkEndpointValid); connect(d->ui.presharedKeyLineEdit, &PasswordField::textChanged, this, &WireGuardPeerWidget::checkPresharedKeyValid); connect(d->ui.presharedKeyLineEdit, &PasswordField::passwordOptionChanged, this, &WireGuardPeerWidget::saveKeyFlags); connect(d->ui.keepaliveLineEdit, &QLineEdit::textChanged, this, &WireGuardPeerWidget::saveKeepAlive); d->ui.presharedKeyLineEdit->setPasswordModeEnabled(true); d->ui.presharedKeyLineEdit->setPasswordOptionsEnabled(true); d->ui.presharedKeyLineEdit->setPasswordNotRequiredEnabled(true); d->ui.presharedKeyLineEdit->setPasswordNotSavedEnabled(false); // Create validator for endpoint port QIntValidator *portValidator = new QIntValidator(this); portValidator->setBottom(0); portValidator->setTop(65535); d->ui.endpointPortLineEdit->setValidator(portValidator); // Reuse the port validator for the persistent keepalive. // It is not a "port" but has the same limits d->ui.keepaliveLineEdit->setValidator(portValidator); KAcceleratorManager::manage(this); updatePeerWidgets(); // Set the initial backgrounds on all the widgets checkPublicKeyValid(); checkAllowedIpsValid(); checkEndpointValid(); } WireGuardPeerWidget::~WireGuardPeerWidget() { delete d; } QVariantMap WireGuardPeerWidget::setting() const { return d->peerData; } void WireGuardPeerWidget::checkPublicKeyValid() { int pos = 0; QLineEdit *widget = d->ui.publicKeyLineEdit; QString value = widget->displayText(); bool valid = QValidator::Acceptable == keyValidator.validate(value, pos); setBackground(widget, valid); d->peerData[PNM_WG_PEER_KEY_PUBLIC_KEY] = value; if (valid != d->publicKeyValid) { d->publicKeyValid = valid; slotWidgetChanged(); } } void WireGuardPeerWidget::checkPresharedKeyValid() { int pos = 0; PasswordField *widget = d->ui.presharedKeyLineEdit; QString value = widget->text(); // The value in the preshared key field is ignored if the type // of password is set to "Ask every time" or "Not Required" so // it is valid if it is set to "Store for user only" // or "Store for all users" even if the password is bad PasswordField::PasswordOption option = d->ui.presharedKeyLineEdit->passwordOption(); bool valid = (QValidator::Acceptable == keyValidator.validate(value, pos) || option == PasswordField::NotRequired); setBackground(widget, valid); if (value.isEmpty()) d->peerData.remove(PNM_WG_PEER_KEY_PRESHARED_KEY); else d->peerData[PNM_WG_PEER_KEY_PRESHARED_KEY] = value; if (valid != d->presharedKeyValid) { d->presharedKeyValid = valid; slotWidgetChanged(); } } void WireGuardPeerWidget::checkAllowedIpsValid() { int pos = 0; QLineEdit *widget = d->ui.allowedIPsLineEdit; QString ipString = widget->displayText(); QStringList ipList = ipString.split(','); bool valid = QValidator::Acceptable == allowedIPsValidator.validate(ipString, pos); setBackground(widget, valid); d->peerData[PNM_WG_PEER_KEY_ALLOWED_IPS] = ipList; if (valid != d->allowedIPsValid) { d->allowedIPsValid = valid; slotWidgetChanged(); } } WireGuardPeerWidget::EndPointValid WireGuardPeerWidget::isEndpointValid(QString &address, QString &port) { // Create a Reg Expression validator to do simple check for a valid qualified domain name // which checks to see that there are between 2 and 63 strings of at least 2 characters each // separated by '.', so "ab.cc" is valid but "a.cc" is not. The full string must also be less // than 255 characters long. static QRegExpValidator fqdnValidator(QRegExp(QLatin1String("(?=.{5,254}$)([a-zA-Z0-9][a-zA-Z0-9-]{1,62}\\.){1,63}[a-zA-Z]{2,63}")), nullptr); static SimpleIpV4AddressValidator ipv4Validator; static SimpleIpV6AddressValidator ipv6Validator; int pos = 0; bool addressValid = QValidator::Acceptable == fqdnValidator.validate(address, pos) || QValidator::Acceptable == ipv4Validator.validate(address, pos) || QValidator::Acceptable == ipv6Validator.validate(address, pos); bool bothEmpty = address.isEmpty() && port.isEmpty(); // Because of the validator, if the port is non-empty, it is valid bool portValid = !port.isEmpty(); if ((portValid && addressValid) || bothEmpty) return WireGuardPeerWidget::BothValid; else if (portValid) return WireGuardPeerWidget::PortValid; else if (addressValid) return WireGuardPeerWidget::AddressValid; else return WireGuardPeerWidget::BothInvalid; } void WireGuardPeerWidget::checkEndpointValid() { QLineEdit *addressWidget = d->ui.endpointAddressLineEdit; QLineEdit *portWidget = d->ui.endpointPortLineEdit; QString addressString = addressWidget->displayText(); QString portString = portWidget->displayText(); QUrl temp; WireGuardPeerWidget::EndPointValid valid = isEndpointValid(addressString, portString); setBackground(addressWidget, WireGuardPeerWidget::BothValid == valid || WireGuardPeerWidget::AddressValid == valid); setBackground(portWidget, WireGuardPeerWidget::BothValid == valid || WireGuardPeerWidget::PortValid == valid); // If there is a ':' in the address string then it is an IPv6 address and // the output needs to be formatted as '[1:2:3:4:5:6:7:8]:123' otherwhise // it is formatted as '1.2.3.4:123' or 'ab.com:123' QString stringToStore; if (addressString.contains(":")) stringToStore = "[" + addressString.trimmed() + "]:" + portString.trimmed(); else stringToStore = addressString.trimmed() + ":" + portString.trimmed(); if (addressString.isEmpty() && portString.isEmpty()) d->peerData.remove(PNM_WG_PEER_KEY_ENDPOINT); else d->peerData[PNM_WG_PEER_KEY_ENDPOINT] = stringToStore; if ((valid == WireGuardPeerWidget::BothValid) != d->endpointValid) { d->endpointValid = (valid == WireGuardPeerWidget::BothValid); slotWidgetChanged(); } } bool WireGuardPeerWidget::isValid() { return d->publicKeyValid && d->allowedIPsValid && d->endpointValid && d->presharedKeyValid; } void WireGuardPeerWidget::saveKeepAlive() { QLineEdit *widget = d->ui.keepaliveLineEdit; QString value = widget->displayText(); if (value.isEmpty()) d->peerData.remove(PNM_WG_PEER_KEY_PERSISTENT_KEEPALIVE); else d->peerData[PNM_WG_PEER_KEY_PERSISTENT_KEEPALIVE] = value; } void WireGuardPeerWidget::saveKeyFlags() { PasswordField::PasswordOption option = d->ui.presharedKeyLineEdit->passwordOption(); switch (option) { case PasswordField::StoreForUser: d->peerData[PNM_WG_PEER_KEY_PRESHARED_KEY_FLAGS] = NetworkManager::Setting::AgentOwned; break; case PasswordField::StoreForAllUsers: d->peerData[PNM_WG_PEER_KEY_PRESHARED_KEY_FLAGS] = NetworkManager::Setting::None; break; // Always Ask is not a valid option for the preshared key so set it to AgentOwned instead case PasswordField::AlwaysAsk: d->peerData[PNM_WG_PEER_KEY_PRESHARED_KEY_FLAGS] = NetworkManager::Setting::AgentOwned; break; case PasswordField::NotRequired: d->peerData[PNM_WG_PEER_KEY_PRESHARED_KEY_FLAGS] = NetworkManager::Setting::NotRequired; break; } checkPresharedKeyValid(); } void WireGuardPeerWidget::setBackground(QWidget *w, bool result) const { if (result) w->setPalette(d->normalPalette); else w->setPalette(d->warningPalette); } void WireGuardPeerWidget::updatePeerWidgets() { d->ui.presharedKeyLineEdit->setPasswordModeEnabled(true); if (d->peerData.contains(PNM_WG_PEER_KEY_PUBLIC_KEY)) d->ui.publicKeyLineEdit->setText(d->peerData[PNM_WG_PEER_KEY_PUBLIC_KEY].toString()); else d->ui.publicKeyLineEdit->clear(); if (d->peerData.contains(PNM_WG_PEER_KEY_ALLOWED_IPS)) { QStringList allowedIps = d->peerData[PNM_WG_PEER_KEY_ALLOWED_IPS].toStringList(); d->ui.allowedIPsLineEdit->setText(allowedIps.join(",")); } else { d->ui.allowedIPsLineEdit->clear(); } if (d->peerData.contains(PNM_WG_PEER_KEY_PERSISTENT_KEEPALIVE)) d->ui.keepaliveLineEdit->setText(d->peerData[PNM_WG_PEER_KEY_PERSISTENT_KEEPALIVE].toString()); else d->ui.keepaliveLineEdit->clear(); // An endpoint is stored as : if (d->peerData.contains(PNM_WG_PEER_KEY_ENDPOINT)) { QString storedEndpoint = d->peerData[PNM_WG_PEER_KEY_ENDPOINT].toString(); QStringList endpointList = storedEndpoint.contains("]:") ? d->peerData[PNM_WG_PEER_KEY_ENDPOINT].toString().split("]:") : d->peerData[PNM_WG_PEER_KEY_ENDPOINT].toString().split(":"); d->ui.endpointAddressLineEdit->setText(endpointList[0].remove("[")); d->ui.endpointPortLineEdit->setText(endpointList[1]); } else { d->ui.endpointAddressLineEdit->clear(); d->ui.endpointPortLineEdit->clear(); } if (d->peerData.contains(PNM_WG_PEER_KEY_PRESHARED_KEY)) d->ui.presharedKeyLineEdit->setText(d->peerData[PNM_WG_PEER_KEY_PRESHARED_KEY].toString()); else d->ui.presharedKeyLineEdit->setText(""); if (d->peerData.contains(PNM_WG_PEER_KEY_PRESHARED_KEY_FLAGS)) { NetworkManager::Setting::SecretFlags type = static_cast(d->peerData[PNM_WG_PEER_KEY_PRESHARED_KEY_FLAGS].toUInt()); switch (type) { case NetworkManager::Setting::AgentOwned: d->ui.presharedKeyLineEdit->setPasswordOption(PasswordField::StoreForUser); break; case NetworkManager::Setting::None: d->ui.presharedKeyLineEdit->setPasswordOption(PasswordField::StoreForAllUsers); break; // Not saved is not a valid option for the private key so set it to StoreForUser instead case NetworkManager::Setting::NotSaved: d->ui.presharedKeyLineEdit->setPasswordOption(PasswordField::StoreForUser); break; case NetworkManager::Setting::NotRequired: d->ui.presharedKeyLineEdit->setPasswordOption(PasswordField::NotRequired); break; } } else { d->ui.presharedKeyLineEdit->setPasswordOption(PasswordField::NotRequired); } slotWidgetChanged(); } void WireGuardPeerWidget::slotWidgetChanged() { Q_EMIT notifyValid(); }