File indexing completed on 2025-04-27 14:24:22
0001 /* 0002 SPDX-FileCopyrightText: 2011 Ilia Kats <ilia-kats@gmx.net> 0003 SPDX-FileCopyrightText: 2013 Lukas Tinkl <ltinkl@redhat.com> 0004 0005 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0006 */ 0007 0008 #include "strongswanauth.h" 0009 #include "nm-strongswan-service.h" 0010 #include "ui_strongswanauth.h" 0011 0012 #include <KMessageBox> 0013 #include <QDialog> 0014 #include <QProcessEnvironment> 0015 #include <QString> 0016 0017 #include <KLocalizedString> 0018 0019 class StrongswanAuthWidgetPrivate 0020 { 0021 public: 0022 Ui_StrongswanAuth ui; 0023 bool acceptOnShow; 0024 NetworkManager::VpnSetting::Ptr setting; 0025 }; 0026 0027 StrongswanAuthWidget::StrongswanAuthWidget(const NetworkManager::VpnSetting::Ptr &setting, const QStringList &hints, QWidget *parent) 0028 : SettingWidget(setting, hints, parent) 0029 , d_ptr(new StrongswanAuthWidgetPrivate) 0030 { 0031 Q_D(StrongswanAuthWidget); 0032 d->setting = setting; 0033 d->ui.setupUi(this); 0034 d->acceptOnShow = false; 0035 0036 readSecrets(); 0037 0038 KAcceleratorManager::manage(this); 0039 } 0040 0041 StrongswanAuthWidget::~StrongswanAuthWidget() 0042 { 0043 delete d_ptr; 0044 } 0045 0046 void StrongswanAuthWidget::readSecrets() 0047 { 0048 Q_D(StrongswanAuthWidget); 0049 const NMStringMap dataMap = d->setting->data(); 0050 0051 const QString method = dataMap[NM_STRONGSWAN_METHOD]; 0052 if (method == QLatin1String(NM_STRONGSWAN_AUTH_AGENT) || dataMap[NM_STRONGSWAN_SECRET_TYPE] == QLatin1String(NM_STRONGSWAN_PW_TYPE_UNUSED)) { 0053 if (isVisible()) { 0054 acceptDialog(); 0055 } else { 0056 d->acceptOnShow = true; 0057 } 0058 } else if (method == QLatin1String(NM_STRONGSWAN_AUTH_KEY)) { 0059 d->ui.passwordLabel->setText(i18nc("@label:textbox password label for private key password", "Private Key Password:")); 0060 } else if (method == QLatin1String(NM_STRONGSWAN_AUTH_SMARTCARD)) { 0061 d->ui.passwordLabel->setText(i18nc("@label:textbox password label for smartcard pin", "PIN:")); 0062 } else if (method == QLatin1String(NM_STRONGSWAN_AUTH_EAP)) { 0063 d->ui.passwordLabel->setText(i18nc("@label:textbox password label for EAP password", "Password:")); 0064 } 0065 } 0066 0067 void StrongswanAuthWidget::setVisible(bool visible) 0068 { 0069 Q_D(StrongswanAuthWidget); 0070 0071 SettingWidget::setVisible(visible); 0072 0073 if (visible) { 0074 if (d->acceptOnShow) { 0075 acceptDialog(); 0076 } else { 0077 SettingWidget::setVisible(visible); 0078 } 0079 } else { 0080 SettingWidget::setVisible(visible); 0081 } 0082 } 0083 0084 void StrongswanAuthWidget::acceptDialog() 0085 { 0086 auto dialog = qobject_cast<QDialog *>(parentWidget()); 0087 if (dialog) { 0088 dialog->accept(); 0089 } 0090 } 0091 0092 QVariantMap StrongswanAuthWidget::setting() const 0093 { 0094 Q_D(const StrongswanAuthWidget); 0095 0096 NMStringMap secrets; 0097 QVariantMap secretData; 0098 0099 if (d->setting->data()[NM_STRONGSWAN_METHOD] == QLatin1String(NM_STRONGSWAN_AUTH_AGENT)) { 0100 const QString agent = QProcessEnvironment::systemEnvironment().value(QLatin1String("SSH_AUTH_SOCK")); 0101 if (!agent.isEmpty()) { 0102 secrets.insert(NM_STRONGSWAN_AUTH_AGENT, agent); 0103 } else { 0104 KMessageBox::error(nullptr, 0105 i18nc("@label:textbox error message while saving configuration", 0106 "Configuration uses ssh-agent for authentication, but no ssh-agent found running.")); 0107 } 0108 } else { 0109 secrets.insert(NM_STRONGSWAN_SECRET, d->ui.password->text()); 0110 } 0111 0112 secretData.insert("secrets", QVariant::fromValue<NMStringMap>(secrets)); 0113 return secretData; 0114 }