File indexing completed on 2024-04-28 16:52:52

0001 /*
0002     SPDX-FileCopyrightText: 2008 Will Stephenson <wstephenson@kde.org>
0003     SPDX-FileCopyrightText: 2010 Maurus Rohrer <maurus.rohrer@gmail.com>
0004     SPDX-FileCopyrightText: 2013 Lukas Tinkl <ltinkl@redhat.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "strongswanwidget.h"
0010 #include "nm-strongswan-service.h"
0011 #include "ui_strongswanprop.h"
0012 
0013 #include <QString>
0014 #include <QUrl>
0015 
0016 class StrongswanSettingWidgetPrivate
0017 {
0018 public:
0019     Ui_StrongswanProp ui;
0020     NetworkManager::VpnSetting::Ptr setting;
0021     enum AuthType { PrivateKey = 0, SshAgent, Smartcard, Eap };
0022 };
0023 
0024 StrongswanSettingWidget::StrongswanSettingWidget(const NetworkManager::VpnSetting::Ptr &setting, QWidget *parent)
0025     : SettingWidget(setting, parent)
0026     , d_ptr(new StrongswanSettingWidgetPrivate)
0027 {
0028     Q_D(StrongswanSettingWidget);
0029     d->ui.setupUi(this);
0030 
0031     d->setting = setting;
0032 
0033     // Connect for setting check
0034     watchChangedSetting();
0035 
0036     // Connect for validity check
0037     connect(d->ui.leGateway, &QLineEdit::textChanged, this, &StrongswanSettingWidget::slotWidgetChanged);
0038     connect(d->ui.proposal, &QGroupBox::toggled, this, &SettingWidget::settingChanged);
0039 
0040     KAcceleratorManager::manage(this);
0041 
0042     if (d->setting && !d->setting->isNull()) {
0043         loadConfig(d->setting);
0044     }
0045 }
0046 
0047 StrongswanSettingWidget::~StrongswanSettingWidget()
0048 {
0049     delete d_ptr;
0050 }
0051 
0052 void StrongswanSettingWidget::loadConfig(const NetworkManager::Setting::Ptr &setting)
0053 {
0054     Q_UNUSED(setting)
0055     Q_D(StrongswanSettingWidget);
0056 
0057     // General settings
0058     const NMStringMap dataMap = d->setting->data();
0059     // Gateway Address
0060     const QString gateway = dataMap[NM_STRONGSWAN_GATEWAY];
0061     if (!gateway.isEmpty()) {
0062         d->ui.leGateway->setText(gateway);
0063     }
0064     // Certificate
0065     d->ui.leGatewayCertificate->setUrl(QUrl::fromLocalFile(dataMap[NM_STRONGSWAN_CERTIFICATE]));
0066 
0067     // Authentication
0068     const QString method = dataMap[NM_STRONGSWAN_METHOD];
0069     if (method == QLatin1String(NM_STRONGSWAN_AUTH_KEY)) {
0070         d->ui.cmbMethod->setCurrentIndex(StrongswanSettingWidgetPrivate::PrivateKey);
0071         d->ui.leAuthPrivatekeyCertificate->setUrl(QUrl::fromLocalFile(dataMap[NM_STRONGSWAN_USERCERT]));
0072         d->ui.leAuthPrivatekeyKey->setUrl(QUrl::fromLocalFile(dataMap[NM_STRONGSWAN_USERKEY]));
0073     } else if (method == QLatin1String(NM_STRONGSWAN_AUTH_AGENT)) {
0074         d->ui.cmbMethod->setCurrentIndex(StrongswanSettingWidgetPrivate::SshAgent);
0075         d->ui.leAuthSshCertificate->setUrl(QUrl::fromLocalFile(dataMap[NM_STRONGSWAN_USERCERT]));
0076     } else if (method == QLatin1String(NM_STRONGSWAN_AUTH_SMARTCARD)) {
0077         d->ui.cmbMethod->setCurrentIndex(StrongswanSettingWidgetPrivate::Smartcard);
0078     } else if (method == QLatin1String(NM_STRONGSWAN_AUTH_EAP)) {
0079         d->ui.cmbMethod->setCurrentIndex(StrongswanSettingWidgetPrivate::Eap);
0080         d->ui.leUserName->setText(dataMap[NM_STRONGSWAN_USER]);
0081     }
0082 
0083     // Settings
0084     d->ui.innerIP->setChecked(dataMap[NM_STRONGSWAN_INNERIP] == "yes");
0085     d->ui.udpEncap->setChecked(dataMap[NM_STRONGSWAN_ENCAP] == "yes");
0086     d->ui.ipComp->setChecked(dataMap[NM_STRONGSWAN_IPCOMP] == "yes");
0087     d->ui.proposal->setChecked(dataMap[NM_STRONGSWAN_PROPOSAL] == "yes");
0088     d->ui.ike->setText(dataMap[NM_STRONGSWAN_IKE]);
0089     d->ui.esp->setText(dataMap[NM_STRONGSWAN_ESP]);
0090 }
0091 
0092 void StrongswanSettingWidget::loadSecrets(const NetworkManager::Setting::Ptr &setting)
0093 {
0094     Q_D(StrongswanSettingWidget);
0095     Q_UNUSED(setting);
0096 }
0097 
0098 QVariantMap StrongswanSettingWidget::setting() const
0099 {
0100     Q_D(const StrongswanSettingWidget);
0101 
0102     NetworkManager::VpnSetting setting;
0103     setting.setServiceType(QLatin1String(NM_DBUS_SERVICE_STRONGSWAN));
0104 
0105     NMStringMap data;
0106     NMStringMap secretData;
0107 
0108     // General settings
0109     // Gateway
0110     if (!d->ui.leGateway->text().isEmpty()) {
0111         data.insert(NM_STRONGSWAN_GATEWAY, d->ui.leGateway->text());
0112     }
0113 
0114     const QString certificate = d->ui.leGatewayCertificate->url().toLocalFile();
0115     if (!certificate.isEmpty()) {
0116         data.insert(NM_STRONGSWAN_CERTIFICATE, certificate);
0117     }
0118 
0119     // Authentication
0120     switch (d->ui.cmbMethod->currentIndex()) {
0121     case StrongswanSettingWidgetPrivate::PrivateKey: {
0122         data.insert(NM_STRONGSWAN_METHOD, NM_STRONGSWAN_AUTH_KEY);
0123         const QString userPrivateCertificate = d->ui.leAuthPrivatekeyCertificate->url().toLocalFile();
0124         if (!userPrivateCertificate.isEmpty()) {
0125             data.insert(NM_STRONGSWAN_USERCERT, userPrivateCertificate);
0126         }
0127         const QString userKey = d->ui.leAuthPrivatekeyKey->url().toLocalFile();
0128         if (!userKey.isEmpty()) {
0129             data.insert(NM_STRONGSWAN_USERKEY, userKey);
0130         }
0131         break;
0132     }
0133     case StrongswanSettingWidgetPrivate::SshAgent: {
0134         data.insert(NM_STRONGSWAN_METHOD, NM_STRONGSWAN_AUTH_AGENT);
0135         const QString userSshCertificate = d->ui.leAuthSshCertificate->url().toLocalFile();
0136         if (!userSshCertificate.isEmpty()) {
0137             data.insert(NM_STRONGSWAN_USERCERT, userSshCertificate);
0138         }
0139         break;
0140     }
0141     case StrongswanSettingWidgetPrivate::Smartcard:
0142         data.insert(NM_STRONGSWAN_METHOD, NM_STRONGSWAN_AUTH_SMARTCARD);
0143         break;
0144     case StrongswanSettingWidgetPrivate::Eap:
0145         data.insert(NM_STRONGSWAN_METHOD, NM_STRONGSWAN_AUTH_EAP);
0146         if (!d->ui.leUserName->text().isEmpty()) {
0147             data.insert(NM_STRONGSWAN_USER, d->ui.leUserName->text());
0148         }
0149         // StrongSwan-nm 1.2 does not appear to be able to save secrets, the must be entered through the auth dialog
0150     }
0151 
0152     // Options
0153     data.insert(NM_STRONGSWAN_INNERIP, d->ui.innerIP->isChecked() ? "yes" : "no");
0154     data.insert(NM_STRONGSWAN_ENCAP, d->ui.udpEncap->isChecked() ? "yes" : "no");
0155     data.insert(NM_STRONGSWAN_IPCOMP, d->ui.ipComp->isChecked() ? "yes" : "no");
0156     if (d->ui.proposal->isChecked()) {
0157         data.insert(NM_STRONGSWAN_PROPOSAL, "yes");
0158         data.insert(NM_STRONGSWAN_IKE, d->ui.ike->text());
0159         data.insert(NM_STRONGSWAN_ESP, d->ui.esp->text());
0160     } else
0161         data.insert(NM_STRONGSWAN_PROPOSAL, "no");
0162 
0163     // save it all
0164     setting.setData(data);
0165     setting.setSecrets(secretData);
0166 
0167     return setting.toMap();
0168 }
0169 
0170 bool StrongswanSettingWidget::isValid() const
0171 {
0172     Q_D(const StrongswanSettingWidget);
0173     return !d->ui.leGateway->text().isEmpty();
0174 }