Branch data Line data Source code
1 : : // SPDX-FileCopyrightText: 2014 Anne Jan Brouwer
2 : : // SPDX-License-Identifier: GPL-3.0-or-later
3 : : /**
4 : : * @file
5 : : * @brief Implements ConfigDialog: loading and reading the widgets
6 : : * (applySettings(), readSettings()), the profile table with its validation
7 : : * (validate(), problemsOf(), sharingTheKey()) and the initialisation of
8 : : * stores for profiles added in the dialog (initializeNewProfiles()).
9 : : */
10 : : #include "configdialog.h"
11 : : #include "appsettings.h"
12 : : #include "executor.h"
13 : : #include "gpgkeystate.h"
14 : : #include "keygendialog.h"
15 : : #include "plainmessagebox.h"
16 : : #include "profileinit.h"
17 : : #include "qtpasssettings.h"
18 : : #include "sshauthsock.h"
19 : : #include "ui_configdialog.h"
20 : : #include "usersdialog.h"
21 : : #include "util.h"
22 : : #include "windowstatestore.h"
23 : : #include <QClipboard>
24 : : #include <QDir>
25 : : #include <QEvent>
26 : : #include <QFile>
27 : : #include <QFileDialog>
28 : : #include <QFileInfo>
29 : : #include <QFormLayout>
30 : : #include <QGuiApplication>
31 : : #include <QHash>
32 : : #include <QLabel>
33 : : #include <QListWidgetItem>
34 : : #include <QMessageBox>
35 : : #include <QPushButton>
36 : : #include <QRegularExpression>
37 : : #include <QSet>
38 : : #include <QSystemTrayIcon>
39 : : #include <algorithm>
40 : : #include <utility>
41 : : #ifdef Q_OS_WIN
42 : : #include <windows.h>
43 : : #endif
44 : :
45 : : #include "qtpasslogging.h"
46 : :
47 : 90 : ConfigDialog::ConfigDialog(QWidget *parent)
48 : 90 : : QDialog(parent), ui(new Ui::ConfigDialog) {
49 : 90 : ui->setupUi(this);
50 : :
51 : 180 : WindowStateStore::attach(*this, QStringLiteral("configDialog"));
52 : :
53 : : // setSpecialValueText() in retranslateUi() drops the size hint without
54 : : // telling the layout, which then sizes the box for "0" and shows "ver".
55 : 90 : ui->spinBoxAutoclearSeconds->updateGeometry();
56 : 90 : ui->spinBoxAutoclearPanelSeconds->updateGeometry();
57 : :
58 : : // The secret field names only mean something while passwords are hidden.
59 : 90 : connect(ui->checkBoxHidePassword, &QCheckBox::toggled, this,
60 : 120 : [this] { updateSecretFieldsEnabled(); });
61 : 90 : connect(ui->checkBoxHideSecretFields, &QCheckBox::toggled, this,
62 : 183 : [this] { updateSecretFieldsEnabled(); });
63 : 90 : connect(ui->checkBoxHideAllFields, &QCheckBox::toggled, this,
64 : 93 : [this] { updateSecretFieldsEnabled(); });
65 : 90 : connect(ui->buttonDefaultSecretFields, &QPushButton::clicked, this, [this] {
66 : 2 : ui->lineEditSecretFields->setText(
67 : 2 : AppSettings::defaultSecretFieldNames().join(QStringLiteral(", ")));
68 : 1 : });
69 : : // "Seconds" says nothing next to "Never" (the spin box's 0).
70 : : // Restoring is part of clearing: only there when the clipboard is cleared.
71 : 90 : connect(ui->spinBoxAutoclearSeconds, &QSpinBox::valueChanged, this,
72 : 90 : [this](int value) {
73 : 33 : ui->labelSeconds->setVisible(value > 0);
74 : 33 : ui->checkBoxRestoreClipboard->setVisible(value > 0);
75 : 33 : });
76 : 90 : connect(ui->spinBoxAutoclearPanelSeconds, &QSpinBox::valueChanged, this,
77 : 125 : [this](int value) { ui->labelPanelSeconds->setVisible(value > 0); });
78 : 90 : connect(
79 : 90 : ui->comboBoxFields, &QComboBox::currentIndexChanged, this,
80 : 127 : [this](int index) { ui->plainTextEditTemplate->setEnabled(index > 0); });
81 : :
82 : 90 : const AppSettings s = QtPassSettings::load();
83 : 90 : applySettings(s);
84 : :
85 [ + + - ]: 90 : if (!QSystemTrayIcon::isSystemTrayAvailable()) {
86 : 90 : ui->checkBoxUseTrayIcon->setEnabled(false);
87 : 180 : ui->checkBoxUseTrayIcon->setToolTip(tr("System tray is not available"));
88 : 90 : ui->checkBoxHideOnClose->setEnabled(false);
89 : 90 : ui->checkBoxStartMinimized->setEnabled(false);
90 : : }
91 : :
92 : : // Wayland has no client-side "keep above" — Qt::WindowStaysOnTopHint is
93 : : // silently ignored there (stacking belongs to the compositor). Say so
94 : : // instead of offering a checkbox that does nothing.
95 [ + + - + ]: 90 : if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"))) {
96 : 0 : ui->checkBoxAlwaysOnTop->setEnabled(false);
97 : 0 : ui->checkBoxAlwaysOnTop->setToolTip(
98 : 0 : tr("Not available on Wayland; use your compositor's "
99 : : "\"keep above\" window rule instead"));
100 : : }
101 : :
102 : : #if defined(Q_OS_WIN)
103 : : // checkBoxUseOtp stays visible: OTP is generated in-process, not by the
104 : : // Unix-only pass-otp extension.
105 : : ui->checkBoxUseQrencode->hide();
106 : : #endif
107 : :
108 [ + + + ]: 90 : if (!isQrencodeAvailable(s.qrencodeExecutable)) {
109 : 88 : ui->checkBoxUseQrencode->setEnabled(false);
110 : 176 : ui->checkBoxUseQrencode->setToolTip(tr("qrencode needs to be installed"));
111 : : }
112 : :
113 : 90 : connect(ui->profileList, &QListWidget::currentRowChanged, this,
114 : 90 : &ConfigDialog::onProfileSelected);
115 : 90 : connect(ui->profileName, &QLineEdit::textEdited, this,
116 : 90 : &ConfigDialog::onProfileNameEdited);
117 : 90 : connect(ui->profilePath, &QLineEdit::textEdited, this,
118 : 90 : &ConfigDialog::onProfilePathEdited);
119 : 90 : connect(ui->profileSigningKey, &QLineEdit::textEdited, this,
120 : 180 : &ConfigDialog::onProfileSigningKeyEdited);
121 : 630 : for (QCheckBox *box :
122 [ + + ]: 360 : {ui->profileUseGit, ui->profileAutoPush, ui->profileAutoPull}) {
123 : 270 : connect(box, &QCheckBox::clicked, this, &ConfigDialog::onProfileGitToggled);
124 : : }
125 : 180 : m_nameTip = ui->profileName->toolTip();
126 : 180 : m_pathTip = ui->profilePath->toolTip();
127 : 90 : m_keyTip = ui->profileSigningKey->toolTip();
128 : : // A warning, not a problem: shown under the signing key while another
129 : : // profile, for another store, signs with the same key (#2050).
130 : 90 : m_sharedKeyWarning = new QLabel(this);
131 : 180 : m_sharedKeyWarning->setObjectName(QStringLiteral("profileSharedKeyWarning"));
132 : 90 : m_sharedKeyWarning->setTextFormat(Qt::PlainText);
133 : 90 : m_sharedKeyWarning->setWordWrap(true);
134 : 90 : m_sharedKeyWarning->hide();
135 : : {
136 : 90 : int row = -1;
137 : 90 : QFormLayout::ItemRole role = QFormLayout::FieldRole;
138 : 90 : ui->profileFormLayout->getWidgetPosition(ui->profileSigningKey, &row,
139 : : &role);
140 : 90 : ui->profileFormLayout->insertRow(row + 1, QString(), m_sharedKeyWarning);
141 : : }
142 : : // Under the pass settings: a store pass would commit outside of.
143 : 90 : m_passGitWarning = new QLabel(this);
144 : 180 : m_passGitWarning->setObjectName(QStringLiteral("passGitWarning"));
145 : 90 : m_passGitWarning->setTextFormat(Qt::PlainText);
146 : 90 : m_passGitWarning->setWordWrap(true);
147 : 90 : m_passGitWarning->hide();
148 : 90 : ui->groupBoxPass->layout()->addWidget(m_passGitWarning);
149 : 90 : connect(ui->storePath, &QLineEdit::textChanged, this,
150 : 101 : [this] { updatePassGitWarning(); });
151 : 90 : connect(ui->pwgenPath, &QLineEdit::textChanged, this,
152 : 90 : &ConfigDialog::updatePwgenCheckbox);
153 : 90 : connect(ui->gitPath, &QLineEdit::textChanged, this,
154 [ + + ]: 180 : &ConfigDialog::updateGitCheckbox);
155 : 270 : setProfiles(QtPassSettings::getProfiles(), QtPassSettings::getProfile());
156 : :
157 : 180 : ui->label->setText(ui->label->text() + VERSION);
158 : 90 : ui->comboBoxClipboard->clear();
159 : :
160 : 180 : ui->comboBoxClipboard->addItem(tr("No clipboard"));
161 : 180 : ui->comboBoxClipboard->addItem(tr("Always copy to clipboard"));
162 : 180 : ui->comboBoxClipboard->addItem(tr("On-demand copy to clipboard"));
163 : :
164 : 90 : int currentIndex = static_cast<int>(s.clipBoardType);
165 : 90 : ui->comboBoxClipboard->setCurrentIndex(currentIndex);
166 : 90 : on_comboBoxClipboard_activated(currentIndex);
167 : :
168 : 90 : QClipboard *clip = QApplication::clipboard();
169 [ + + - ]: 90 : if (!clip->supportsSelection()) {
170 : 90 : useSelection(false);
171 : 90 : ui->checkBoxSelection->setVisible(false);
172 : : } else {
173 : 0 : useSelection(s.useSelection);
174 : : }
175 : :
176 : 90 : connect(ui->pageList, &QListWidget::currentRowChanged, ui->pages,
177 [ + + ]: 180 : &QStackedWidget::setCurrentIndex);
178 [ + + + ]: 174 : ui->pageList->setCurrentRow(
179 : 90 : Util::configIsValid(s)
180 : : ? 0
181 : : // Programs is what needs fixing when the configuration is not valid.
182 : 84 : : ui->pages->indexOf(ui->pagePrograms));
183 : :
184 : 90 : connect(this, &ConfigDialog::accepted, this, &ConfigDialog::on_accepted);
185 : 90 : }
186 : :
187 : 90 : void ConfigDialog::applySettings(const AppSettings &settings) {
188 : 90 : ui->passPath->setText(settings.passExecutable);
189 : 90 : setGitPath(settings.gitExecutable);
190 : 90 : ui->gpgPath->setText(settings.gpgExecutable);
191 : 90 : ui->storePath->setText(settings.passStore);
192 : 90 : ui->sshAuthSockOverride->setText(settings.sshAuthSockOverride);
193 : :
194 : : // One control each: 0 is "Never", so off and a delay are one setting.
195 : 90 : ui->spinBoxAutoclearSeconds->setValue(
196 [ + + ]: 90 : settings.useAutoclear ? settings.autoclearSeconds : 0);
197 : 90 : ui->labelSeconds->setVisible(ui->spinBoxAutoclearSeconds->value() > 0);
198 : 90 : ui->checkBoxRestoreClipboard->setChecked(settings.restoreClipboard);
199 : 90 : ui->checkBoxRestoreClipboard->setVisible(
200 : 90 : ui->spinBoxAutoclearSeconds->value() > 0);
201 : 90 : ui->spinBoxAutoclearPanelSeconds->setValue(
202 [ + + ]: 90 : settings.useAutoclearPanel ? settings.autoclearPanelSeconds : 0);
203 : 90 : ui->labelPanelSeconds->setVisible(ui->spinBoxAutoclearPanelSeconds->value() >
204 : : 0);
205 : 90 : ui->checkBoxHidePassword->setChecked(settings.hidePassword);
206 : 90 : ui->checkBoxHideSecretFields->setChecked(settings.hideSecretFields);
207 : 90 : ui->checkBoxHideAllFields->setChecked(settings.hideAllFields);
208 : 90 : ui->lineEditSecretFields->setText(
209 : 180 : settings.secretFieldNames.join(QStringLiteral(", ")));
210 : 90 : updateSecretFieldsEnabled();
211 : 90 : ui->checkBoxHideContent->setChecked(settings.hideContent);
212 : 90 : ui->checkBoxUseMonospace->setChecked(settings.useMonospace);
213 : 90 : ui->checkBoxDisplayAsIs->setChecked(settings.displayAsIs);
214 : 90 : ui->checkBoxNoLineWrapping->setChecked(settings.noLineWrapping);
215 : 90 : ui->checkBoxAddGPGId->setChecked(settings.addGPGId);
216 : 90 : ui->checkBoxHideOnClose->setChecked(settings.hideOnClose);
217 : 90 : ui->checkBoxStartMinimized->setChecked(settings.startMinimized);
218 : :
219 : 90 : ui->checkBoxAvoidCapitals->setChecked(settings.avoidCapitals);
220 : 90 : ui->checkBoxAvoidNumbers->setChecked(settings.avoidNumbers);
221 : 90 : ui->checkBoxLessRandom->setChecked(settings.lessRandom);
222 : 90 : ui->checkBoxUseSymbols->setChecked(settings.useSymbols);
223 : 90 : ui->plainTextEditTemplate->setPlainText(settings.passTemplate);
224 : : // All fields only applies while templating is on (#1766), so the two
225 : : // switches are one choice of three.
226 : 90 : ui->comboBoxFields->setCurrentIndex(
227 [ + + + + ]: 90 : !settings.useTemplate ? 0 : (settings.templateAllFields ? 2 : 1));
228 : 90 : ui->plainTextEditTemplate->setEnabled(settings.useTemplate);
229 : 90 : ui->checkBoxAutoPull->setChecked(settings.autoPull);
230 : 90 : ui->checkBoxAutoPush->setChecked(settings.autoPush);
231 : 90 : ui->checkBoxGitUseParentRepository->setChecked(
232 : 90 : settings.gitUseParentRepository);
233 : 90 : ui->checkBoxAlwaysOnTop->setChecked(settings.alwaysOnTop);
234 : :
235 : : // Dependent helpers: set after the plain values above so the enable/disable
236 : : // logic they trigger sees the final widget states.
237 : 90 : usePass(settings.usePass);
238 : 90 : useTrayIcon(settings.useTrayIcon);
239 : 90 : useGit(settings.useGit);
240 : 90 : useOtp(settings.useOtp);
241 : 90 : useGrepSearch(settings.useGrepSearch);
242 : 90 : useQrencode(settings.useQrencode);
243 : : // Before usePwgen (gated on the pwgen path); otherwise readSettings() writes
244 : : // the .ui defaults over the saved configuration on every OK.
245 : 90 : setPwgenPath(settings.pwgenExecutable);
246 : 90 : setPasswordConfiguration(settings.passwordConfiguration);
247 : 90 : usePwgen(settings.usePwgen);
248 : 90 : }
249 : :
250 : 30 : auto ConfigDialog::readSettings() -> AppSettings {
251 : : // Start from the persisted settings so keys this dialog does not own
252 : : // (window geometry, profiles, etc.) survive the save.
253 : 30 : AppSettings settings = QtPassSettings::load();
254 : :
255 : 60 : settings.passExecutable = ui->passPath->text();
256 : 60 : settings.gitExecutable = ui->gitPath->text();
257 : 60 : settings.gpgExecutable = ui->gpgPath->text();
258 : 60 : settings.sshAuthSockOverride = ui->sshAuthSockOverride->text().trimmed();
259 : 60 : settings.passStore = Util::normalizeFolderPath(ui->storePath->text());
260 : 30 : settings.usePass = ui->radioButtonPass->isChecked();
261 : 30 : settings.clipBoardType =
262 : 30 : static_cast<Enums::ClipBoardType>(ui->comboBoxClipboard->currentIndex());
263 : 30 : settings.useSelection = ui->checkBoxSelection->isChecked();
264 : 30 : settings.autoclearSeconds = ui->spinBoxAutoclearSeconds->value();
265 : 30 : settings.useAutoclear = settings.autoclearSeconds > 0;
266 : 30 : settings.restoreClipboard = ui->checkBoxRestoreClipboard->isChecked();
267 : 30 : settings.autoclearPanelSeconds = ui->spinBoxAutoclearPanelSeconds->value();
268 : 30 : settings.useAutoclearPanel = settings.autoclearPanelSeconds > 0;
269 : 30 : settings.hidePassword = ui->checkBoxHidePassword->isChecked();
270 : 30 : settings.hideSecretFields = ui->checkBoxHideSecretFields->isChecked();
271 : 30 : settings.hideAllFields = ui->checkBoxHideAllFields->isChecked();
272 : : // As written, an empty list included ("Default" puts the set back).
273 : 30 : settings.secretFieldNames.clear();
274 : : const QStringList names =
275 : 30 : ui->lineEditSecretFields->text().split(QLatin1Char(','));
276 [ + + ]: 283 : for (const QString &name : names) {
277 [ + + ]: 253 : if (!name.trimmed().isEmpty()) {
278 : 240 : settings.secretFieldNames << name.trimmed();
279 : : }
280 : : }
281 : 30 : settings.hideContent = ui->checkBoxHideContent->isChecked();
282 : 30 : settings.useMonospace = ui->checkBoxUseMonospace->isChecked();
283 : 30 : settings.displayAsIs = ui->checkBoxDisplayAsIs->isChecked();
284 : 30 : settings.noLineWrapping = ui->checkBoxNoLineWrapping->isChecked();
285 : 30 : settings.addGPGId = ui->checkBoxAddGPGId->isChecked();
286 : : // A disabled control (e.g. no system tray) keeps the persisted value
287 : : // instead of clobbering it to false.
288 [ + + ]: 30 : if (ui->checkBoxUseTrayIcon->isEnabled()) {
289 : 1 : settings.useTrayIcon = ui->checkBoxUseTrayIcon->isChecked();
290 : : // Switched off here, hide-on-close and start-minimized go off with it.
291 : : // Their boxes are disabled now and skipped below, so a stored "on"
292 : : // would come back the next time anything turns the tray on (the
293 : : // first-run wizard sets useTrayIcon only), and closing would hide.
294 [ + - ]: 1 : if (!settings.useTrayIcon) {
295 : 1 : settings.hideOnClose = false;
296 : 1 : settings.startMinimized = false;
297 : : }
298 : : }
299 [ - + ]: 30 : if (ui->checkBoxHideOnClose->isEnabled()) {
300 : 0 : settings.hideOnClose = ui->checkBoxHideOnClose->isChecked();
301 : : }
302 [ - + ]: 30 : if (ui->checkBoxStartMinimized->isEnabled()) {
303 : 0 : settings.startMinimized = ui->checkBoxStartMinimized->isChecked();
304 : : }
305 : 30 : settings.useGit = ui->checkBoxUseGit->isChecked();
306 : : // The backends and the auto-pull-on-start read the global keys; #1140
307 : : // started storing these per profile as well and stopped writing the
308 : : // globals, which left both checkboxes without effect.
309 : 30 : settings.autoPush = ui->checkBoxAutoPush->isChecked();
310 : 30 : settings.autoPull = ui->checkBoxAutoPull->isChecked();
311 : 30 : settings.gitUseParentRepository =
312 : 30 : ui->checkBoxGitUseParentRepository->isChecked();
313 : 30 : settings.useOtp = ui->checkBoxUseOtp->isChecked();
314 : 30 : settings.useGrepSearch = ui->checkBoxUseGrepSearch->isChecked();
315 : 30 : settings.useQrencode = ui->checkBoxUseQrencode->isChecked();
316 : 60 : settings.pwgenExecutable = ui->pwgenPath->text();
317 : 30 : settings.usePwgen = ui->checkBoxUsePwgen->isChecked();
318 : 30 : settings.avoidCapitals = ui->checkBoxAvoidCapitals->isChecked();
319 : 30 : settings.avoidNumbers = ui->checkBoxAvoidNumbers->isChecked();
320 : 30 : settings.lessRandom = ui->checkBoxLessRandom->isChecked();
321 : 30 : settings.useSymbols = ui->checkBoxUseSymbols->isChecked();
322 : 60 : settings.passwordConfiguration = getPasswordConfiguration();
323 [ + + ]: 30 : settings.useTemplate = ui->comboBoxFields->currentIndex() > 0;
324 : 60 : settings.passTemplate = ui->plainTextEditTemplate->toPlainText();
325 [ + + ]: 30 : settings.templateAllFields = ui->comboBoxFields->currentIndex() == 2;
326 : 30 : settings.alwaysOnTop = ui->checkBoxAlwaysOnTop->isChecked();
327 : 30 : settings.version = VERSION;
328 : :
329 : 30 : return settings;
330 : 0 : }
331 : :
332 : 276 : ConfigDialog::~ConfigDialog() = default;
333 : :
334 : 93 : void ConfigDialog::setGitPath(const QString &path) {
335 : 93 : ui->gitPath->setText(path);
336 : : // setText() is silent when the text did not change.
337 : 93 : updateGitCheckbox();
338 : 93 : }
339 : :
340 : : // Every way to a git path ends here: loading the settings, Autodetect, the
341 : : // file dialog and typing in the field.
342 : 131 : void ConfigDialog::updateGitCheckbox() {
343 : 131 : const bool hasGit = !ui->gitPath->text().isEmpty();
344 [ + + + + ]: 131 : if (!hasGit && ui->checkBoxUseGit->isChecked()) {
345 : : // Replacing the path by hand passes through an empty field. Git going
346 : : // off for good there would stop the commits without a word.
347 : 3 : m_gitOnBeforeEmptyPath = true;
348 : 3 : useGit(false);
349 : : }
350 : 131 : ui->checkBoxUseGit->setEnabled(hasGit);
351 [ + + + + ]: 131 : if (hasGit && m_gitOnBeforeEmptyPath) {
352 : 3 : m_gitOnBeforeEmptyPath = false;
353 : 3 : useGit(true);
354 : : }
355 : 131 : }
356 : :
357 : 92 : void ConfigDialog::usePass(bool usePass) {
358 : 92 : ui->radioButtonNative->setChecked(!usePass);
359 : 92 : ui->radioButtonPass->setChecked(usePass);
360 : 92 : setGroupBoxState();
361 : 92 : }
362 : :
363 : 190 : auto ConfigDialog::isFingerprintList(const QString &setting) -> bool {
364 : : // 40 hex characters for OpenPGP v4 keys, 64 for v5/v6 (gpg 2.5+).
365 : : static const QRegularExpression fingerprint(
366 [ + + + - : 194 : QStringLiteral("^(?:[0-9A-Fa-f]{40}|[0-9A-Fa-f]{64})$"));
+ ]
367 : 190 : const QStringList keys = setting.split(QLatin1Char(' '), Qt::SkipEmptyParts);
368 : 190 : return std::all_of(keys.cbegin(), keys.cend(), [](const QString &key) {
369 : 48 : return fingerprint.match(key).hasMatch();
370 : 190 : });
371 : : }
372 : :
373 : : // Profile names must be unique: the settings key profiles by name, so a
374 : : // duplicate would silently overwrite the other. Unique ignoring case:
375 : : // QSettings keys are case-insensitive on Windows (the registry, and INI
376 : : // files there), so "Work" and "work" were one profile, and a settings file
377 : : // can move between systems. For the same reason a name
378 : : // holds no slash: QSettings reads "/" as a group separator, and "\" as one on
379 : : // Windows, so "Work/Client" was stored a group too deep and read back as a
380 : : // profile "Work" without folder or signing key.
381 : 187 : auto ConfigDialog::problemsOf(const ProfileEntry &entry,
382 : : const QHash<QString, int> &names) const
383 : : -> ProfileProblems {
384 : 187 : ProfileProblems problems;
385 [ + + ]: 187 : if (entry.name.isEmpty()) {
386 : 1 : problems.name = tr("This field is required");
387 [ + + ]: 186 : } else if (entry.name.contains(QLatin1Char('/')) ||
388 [ + + ]: 182 : entry.name.contains(QLatin1Char('\\'))) {
389 : 5 : problems.name = tr("A profile name cannot contain / or \\");
390 [ + + ]: 181 : } else if (names.value(entry.name.toCaseFolded()) > 1) {
391 : 4 : problems.name = tr("Another profile already has this name");
392 : : }
393 [ + + ]: 187 : if (entry.profile.path.isEmpty()) {
394 : 3 : problems.path = tr("This field is required");
395 : : }
396 : : // pass compares PASSWORD_STORE_SIGNING_KEY against the fingerprints in
397 : : // gpg's VALIDSIG line, so anything shorter than a fingerprint would sign
398 : : // and then never verify. Refuse it here instead of failing at first use.
399 [ + + + ]: 187 : if (!isFingerprintList(entry.profile.signingKey)) {
400 : 2 : problems.key = tr("Full key fingerprints only (40 or 64 hexadecimal "
401 : : "characters), separated by spaces");
402 : : }
403 : 187 : return problems;
404 : 0 : }
405 : :
406 : 187 : void ConfigDialog::showProblems(int row, const ProfileProblems &problems) {
407 [ + - ]: 187 : if (QListWidgetItem *item = ui->profileList->item(row)) {
408 [ + + + ]: 187 : item->setBackground(problems.none() ? QBrush() : QBrush(Qt::red));
409 : 374 : item->setToolTip(problems.none() ? problems.sharedKey : problems.first());
410 : : }
411 [ + + ]: 187 : if (row != m_currentEntry) {
412 : : return;
413 : : }
414 : 108 : m_sharedKeyWarning->setText(problems.sharedKey);
415 : 108 : m_sharedKeyWarning->setVisible(!problems.sharedKey.isEmpty());
416 : : const auto tipOr = [](const QString &problem, const QString &tip) -> QString {
417 [ + + + + : 324 : return problem.isEmpty() ? tip : problem;
+ + ]
418 : : };
419 : 324 : ui->profileName->setToolTip(tipOr(problems.name, m_nameTip));
420 : 324 : ui->profilePath->setToolTip(tipOr(problems.path, m_pathTip));
421 : 324 : ui->profileSigningKey->setToolTip(tipOr(problems.key, m_keyTip));
422 : : }
423 : :
424 : 252 : auto ConfigDialog::signingStores() const -> SigningStores {
425 : : // One folder spelled two ways (through a link, another case on a
426 : : // case-insensitive file system) is one store: compared as the file system
427 : : // resolves it where it exists. Resolved once per validate(), not once per
428 : : // pair of profiles.
429 : 252 : SigningStores resolved;
430 : : // Keys are looked up in the keyring that signs: the configured GPG home,
431 : : // as profiles sign with it, not whatever GNUPGHOME QtPass inherited. The
432 : : // dialog does not edit the home, so the settings are read once, not on
433 : : // every keystroke; the environment is built each time, as the home may
434 : : // only appear meanwhile (a key generated from this dialog).
435 [ + + ]: 252 : if (!m_gpgSettings) {
436 : 90 : m_gpgSettings = QtPassSettings::load();
437 : : }
438 : 252 : const QProcessEnvironment env = ProfileInit::gpgEnvironment(*m_gpgSettings);
439 [ + + ]: 439 : for (const ProfileEntry &entry : std::as_const(m_entries)) {
440 : 187 : const QString real = QFileInfo(entry.profile.path).canonicalFilePath();
441 [ + + + ]: 187 : resolved.stores << (real.isEmpty() ? QDir::cleanPath(entry.profile.path)
442 [ + + ]: 187 : : real);
443 : 374 : resolved.keys << signerKeys(entry.profile.signingKey, env);
444 : : }
445 : 252 : return resolved;
446 : 252 : }
447 : :
448 : 187 : auto ConfigDialog::signerKeys(const QString &signingKey,
449 : : const QProcessEnvironment &env) const
450 : : -> QSet<QString> {
451 : : // Verification takes the primary or the signing subkey's fingerprint
452 : : // from VALIDSIG, and gpg signs for a primary key with its subkey: a key
453 : : // written as the subkey in one profile and as the primary in another is
454 : : // one signer (#2087). Only complete IDs are asked about, so typing a key
455 : : // does not start gpg for every prefix.
456 : 2 : static const QRegularExpression complete(QStringLiteral(
457 [ + + + - : 191 : "^(?:0X)?(?:[0-9A-F]{8}|[0-9A-F]{16}|[0-9A-F]{40}|[0-9A-F]{64})$"));
+ ]
458 : 187 : const QString gpg = ui->gpgPath->text();
459 : : QSet<QString> keys;
460 : : const QStringList given =
461 : 187 : signingKey.toUpper().split(QLatin1Char(' '), Qt::SkipEmptyParts);
462 [ + + ]: 233 : for (const QString &key : given) {
463 : 46 : keys.insert(key);
464 [ + + + + : 61 : if (gpg.isEmpty() || !complete.match(key).hasMatch()) {
+ - + + -
- ]
465 : 31 : continue;
466 : : }
467 : 30 : const QString cacheKey = gpg + QLatin1Char('\n') +
468 : 30 : env.value(QStringLiteral("GNUPGHOME")) +
469 : 30 : QLatin1Char('\n') + key;
470 : 15 : auto cached = m_primaryOf.find(cacheKey);
471 : : if (cached == m_primaryOf.end()) {
472 : 9 : QString out;
473 : : // Blocking, once per key: no trustdb check, which a large keyring
474 : : // would make slow.
475 : 99 : const int rc = Executor::executeBlocking(
476 : : env, gpg,
477 : 9 : {QStringLiteral("--no-tty"), QStringLiteral("--batch"),
478 : 9 : QStringLiteral("--no-auto-check-trustdb"),
479 : 9 : QStringLiteral("--with-colons"),
480 : 9 : QStringLiteral("--with-fingerprint"),
481 : 9 : QStringLiteral("--with-subkey-fingerprint"),
482 : 9 : QStringLiteral("--list-keys"), QStringLiteral("--"), key},
483 : : &out);
484 : 9 : cached = m_primaryOf.insert(
485 [ + + + ]: 18 : cacheKey, rc == 0 ? primaryFingerprintOf(out, key) : QString());
486 : : }
487 [ + + ]: 15 : if (!cached->isEmpty()) {
488 : 5 : keys.insert(*cached);
489 : : }
490 : : }
491 : 187 : return keys;
492 : : }
493 : :
494 : 107 : void ConfigDialog::changeEvent(QEvent *event) {
495 : 107 : QDialog::changeEvent(event);
496 [ + + ]: 107 : if (event->type() != QEvent::ActivationChange) {
497 : : return;
498 : : }
499 : : // A key is imported somewhere else: a terminal, a key manager, or the
500 : : // key generator this dialog opens. Whatever keyring gpg uses, the dialog
501 : : // is in the background meanwhile, so a key gpg did not know is asked
502 : : // about again on return.
503 [ + + ]: 17 : if (!isActiveWindow()) {
504 : 7 : const auto forgotten = m_primaryOf.removeIf(
505 : : [](const std::pair<const QString &, QString &> &lookup) {
506 : : return lookup.second.isEmpty();
507 : : });
508 [ + - + + ]: 8 : m_recheckSigners = m_recheckSigners || forgotten > 0;
509 [ + + ]: 10 : } else if (m_recheckSigners) {
510 : 1 : m_recheckSigners = false;
511 : 1 : validate();
512 : : }
513 : : }
514 : :
515 : 187 : auto ConfigDialog::sharingTheKey(int row, const SigningStores &resolved) const
516 : : -> QStringList {
517 : : // A list signed for one store verifies in the other when the stores share
518 : : // a signing key; the folder line names a folder, not a store (#2050).
519 : 187 : QStringList others;
520 : : const QSet<QString> &mine = resolved.keys.at(row);
521 : : if (mine.isEmpty()) {
522 : : return others;
523 : : }
524 [ + + ]: 146 : for (int other = 0; other < m_entries.size(); ++other) {
525 [ + + + + ]: 101 : if (other == row || resolved.stores.at(other) == resolved.stores.at(row)) {
526 : 59 : continue;
527 : : }
528 [ + + ]: 42 : if (resolved.keys.at(other).intersects(mine)) {
529 : 16 : others << m_entries.at(other).name;
530 : : }
531 : : }
532 : : others.sort();
533 : : return others;
534 : : }
535 : :
536 : 252 : void ConfigDialog::validate() {
537 : 252 : QHash<QString, int> names;
538 [ + + ]: 439 : for (const ProfileEntry &entry : std::as_const(m_entries)) {
539 : 374 : names[entry.name.toCaseFolded()]++;
540 : : }
541 : : bool status = true;
542 : 252 : const SigningStores resolved = signingStores();
543 [ + + ]: 439 : for (int row = 0; row < m_entries.size(); ++row) {
544 : 187 : ProfileProblems problems = problemsOf(m_entries.at(row), names);
545 [ + + + ]: 187 : if (const QStringList others = sharingTheKey(row, resolved);
546 : : !others.isEmpty()) {
547 : : problems.sharedKey =
548 : 13 : tr("%1 signs with the same key for another store. A recipient "
549 : : "list signed for one of these stores is accepted in the other: "
550 : : "use a separate signing key for each store whose recipients "
551 : : "differ.")
552 : 26 : .arg(others.join(QStringLiteral(", ")));
553 : : }
554 : 187 : showProblems(row, problems);
555 [ + + ]: 187 : status = status && problems.none();
556 : 187 : }
557 : 252 : ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
558 : 252 : updatePassGitWarning();
559 : 252 : }
560 : :
561 : 374 : void ConfigDialog::updatePassGitWarning() {
562 : : // Built after the first applySettings(); setProfiles() fills it then.
563 [ + + ]: 374 : if (m_passGitWarning == nullptr) {
564 : 90 : return;
565 : : }
566 : 284 : QStringList warnings;
567 [ + + + ]: 284 : if (ui->radioButtonPass->isChecked()) {
568 : 26 : QStringList stores{ui->storePath->text()};
569 [ + + ]: 15 : for (const ProfileEntry &entry : std::as_const(m_entries)) {
570 : 2 : stores << entry.profile.path;
571 : : }
572 : : stores.removeDuplicates();
573 [ + + ]: 28 : for (const QString &store : std::as_const(stores)) {
574 : 15 : const QString repository = Util::repositoryPassClimbsTo(store);
575 [ + + ]: 15 : if (!repository.isEmpty()) {
576 : 5 : warnings << tr("pass commits changes in %1 to the Git repository in "
577 : : "%2: with a colon in the store's path, pass cannot "
578 : : "keep Git to the store. Run \"git init\" in the store, "
579 : : "or use native Git/GPG.")
580 : 15 : .arg(QDir::toNativeSeparators(store),
581 : 10 : QDir::toNativeSeparators(repository));
582 : : }
583 : : }
584 : : }
585 : 568 : m_passGitWarning->setText(warnings.join(QStringLiteral("\n\n")));
586 : 284 : m_passGitWarning->setVisible(!warnings.isEmpty());
587 : 13 : }
588 : :
589 : 30 : void ConfigDialog::warnAboutSshAuthSockOverride() {
590 : 60 : const QString override = ui->sshAuthSockOverride->text().trimmed();
591 [ + + ]: 30 : if (override.isEmpty()) {
592 : : return;
593 : : }
594 : 3 : QString reason;
595 [ + + + + : 3 : switch (SshAuthSock::overrideStatus(override)) {
- - ]
596 : : case SshAuthSock::OverrideStatus::Valid:
597 : : return;
598 : 1 : case SshAuthSock::OverrideStatus::DoesNotExist:
599 : 1 : reason = tr("The path does not exist.");
600 : 1 : break;
601 : 1 : case SshAuthSock::OverrideStatus::NotReadable:
602 : 1 : reason = tr("The path is not readable.");
603 : 1 : break;
604 : 1 : case SshAuthSock::OverrideStatus::NotUnixDomainSocket:
605 : 1 : reason = tr("The path is not a Unix domain socket.");
606 : 1 : break;
607 : : }
608 : 3 : PlainMessageBox::warning(
609 : 3 : this, tr("Potentially invalid SSH_AUTH_SOCK override"),
610 : 3 : tr("The SSH_AUTH_SOCK override value may be invalid.\n\n%1\n\n"
611 : : "The value will still be saved as entered.")
612 : 6 : .arg(reason));
613 : : }
614 : :
615 : 30 : void ConfigDialog::followActiveProfile(const Profiles &profiles) {
616 : : // As MainWindow::on_profileBox_currentTextChanged does when switching.
617 : 30 : AppSettings s = QtPassSettings::load();
618 : : const QString wasActive = s.activeProfile;
619 : 30 : QString nowActive;
620 [ + + ]: 43 : for (const ProfileEntry &entry : std::as_const(m_entries)) {
621 [ + + + + ]: 13 : if (!wasActive.isEmpty() && entry.originalName == wasActive) {
622 : 4 : nowActive = entry.name;
623 : : }
624 : : }
625 : 30 : s.activeProfile = nowActive;
626 : : const auto active = profiles.constFind(nowActive);
627 [ + + ]: 30 : if (active != profiles.constEnd()) {
628 : : // Not the profile's Git flags: those stay the profile's, and the Git
629 : : // page's values stay what the page says (QtPassSettings::loadEffective()).
630 : : // The signing key the active profile carries must become the live setting
631 : : // the backends check .gpg-id signatures against, as
632 : : // MainWindow::on_profileBox_currentTextChanged does when switching. Without
633 : : // this, a key entered for the profile in use is saved but never applied, so
634 : : // QtPass keeps accepting unsigned (or badly signed) .gpg-id lists.
635 : 4 : s.passSigningKey = active->signingKey;
636 : : }
637 : : // A store typed in the store field that is another profile's opens as
638 : : // that profile, with its signing key; one inside a signed profile's store
639 : : // takes that key.
640 : 30 : QtPassSettings::takeProfileOfStore(s, profiles);
641 : 30 : QtPassSettings::save(s);
642 : 30 : }
643 : :
644 : 30 : void ConfigDialog::on_accepted() {
645 : 30 : warnAboutSshAuthSockOverride();
646 : 30 : const Profiles existingProfiles = QtPassSettings::getProfiles();
647 : : // Persist via the facade, which also invalidates the cached Pass backend so
648 : : // a changed "use pass" mode takes effect.
649 : 30 : QtPassSettings::save(readSettings());
650 : : // Profiles are not part of AppSettings yet, so persist them separately.
651 : 30 : const Profiles profiles = getProfiles();
652 : 30 : QtPassSettings::setProfiles(profiles);
653 : 30 : followActiveProfile(profiles);
654 : 30 : initializeNewProfiles(existingProfiles);
655 : 30 : }
656 : :
657 : 2 : void ConfigDialog::on_autodetectButton_clicked() {
658 : 4 : QString pass = Util::findBinaryInPath("pass");
659 [ + + ]: 2 : if (!pass.isEmpty()) {
660 : 1 : ui->passPath->setText(pass);
661 : : }
662 : 2 : usePass(!pass.isEmpty());
663 : 4 : QString gpg = Util::findBinaryInPath("gpg2");
664 [ + - ]: 2 : if (gpg.isEmpty()) {
665 : 4 : gpg = Util::findBinaryInPath("gpg");
666 : : }
667 [ + - ]: 2 : if (!gpg.isEmpty()) {
668 : 2 : ui->gpgPath->setText(gpg);
669 : : }
670 : 4 : QString git = Util::findBinaryInPath("git");
671 [ + - ]: 2 : if (!git.isEmpty()) {
672 : 2 : setGitPath(git);
673 : : }
674 : 4 : QString pwgen = Util::findBinaryInPath("pwgen");
675 [ + - ]: 2 : if (!pwgen.isEmpty()) {
676 : 2 : setPwgenPath(pwgen);
677 : : }
678 : 2 : }
679 : :
680 : 12 : void ConfigDialog::on_radioButtonNative_clicked() { setGroupBoxState(); }
681 : :
682 : 7 : void ConfigDialog::on_radioButtonPass_clicked() { setGroupBoxState(); }
683 : :
684 : 111 : void ConfigDialog::setGroupBoxState() {
685 : 111 : bool state = ui->radioButtonPass->isChecked();
686 : 111 : ui->groupBoxNative->setEnabled(!state);
687 : 111 : ui->groupBoxPass->setEnabled(state);
688 : 111 : updatePassGitWarning();
689 : 111 : on_checkBoxUseGit_clicked();
690 [ + + ]: 111 : if (state) {
691 : 10 : ui->spinBoxPasswordLength->setEnabled(false);
692 : 10 : ui->checkBoxUsePwgen->setEnabled(false);
693 : 10 : ui->checkBoxAvoidCapitals->setEnabled(false);
694 : 10 : ui->checkBoxUseSymbols->setEnabled(false);
695 : 10 : ui->checkBoxLessRandom->setEnabled(false);
696 : 10 : ui->checkBoxAvoidNumbers->setEnabled(false);
697 : 10 : ui->labelPasswordChars->setEnabled(false);
698 : 10 : ui->passwordCharTemplateSelector->setEnabled(false);
699 : 10 : ui->lineEditPasswordChars->setEnabled(false);
700 : : } else {
701 : : // native mode: restore pwgen/charset state from existing handlers
702 : 101 : ui->spinBoxPasswordLength->setEnabled(true);
703 : 101 : updatePwgenCheckbox();
704 : : }
705 : 111 : }
706 : :
707 : 8 : auto ConfigDialog::selectExecutable() -> QString {
708 : 16 : QFileDialog dialog(this);
709 : 8 : dialog.setFileMode(QFileDialog::ExistingFile);
710 : 8 : dialog.setOption(QFileDialog::ReadOnly);
711 [ + + + ]: 8 : if (dialog.exec()) {
712 : 8 : return dialog.selectedFiles().constFirst();
713 : : }
714 : :
715 : 4 : return {};
716 : 8 : }
717 : :
718 : 3 : auto ConfigDialog::selectFolder() -> QString {
719 : 6 : QFileDialog dialog(this);
720 : 3 : dialog.setFileMode(QFileDialog::Directory);
721 : 3 : dialog.setFilter(QDir::NoFilter);
722 : 3 : dialog.setOption(QFileDialog::ShowDirsOnly);
723 [ + + + ]: 3 : if (dialog.exec()) {
724 : 4 : return dialog.selectedFiles().constFirst();
725 : : }
726 : :
727 : 1 : return {};
728 : 3 : }
729 : :
730 : 2 : void ConfigDialog::on_toolButtonGit_clicked() {
731 : 2 : QString git = selectExecutable();
732 [ + + ]: 2 : if (!git.isEmpty()) {
733 : 1 : setGitPath(git);
734 : : }
735 : 2 : }
736 : :
737 : 2 : void ConfigDialog::on_toolButtonGpg_clicked() {
738 : 2 : QString gpg = selectExecutable();
739 [ + + ]: 2 : if (!gpg.isEmpty()) {
740 : 1 : ui->gpgPath->setText(gpg);
741 : : }
742 : 2 : }
743 : :
744 : 1 : void ConfigDialog::on_pushButtonGenerateKey_clicked() {
745 : 1 : KeygenDialog d(ui->gpgPath->text(), QtPassSettings::getPass(), this);
746 : 1 : d.exec();
747 : 1 : }
748 : :
749 : 2 : void ConfigDialog::on_toolButtonPass_clicked() {
750 : 2 : QString pass = selectExecutable();
751 [ + + ]: 2 : if (!pass.isEmpty()) {
752 : 1 : ui->passPath->setText(pass);
753 : : }
754 : 2 : }
755 : :
756 : 2 : void ConfigDialog::on_toolButtonStore_clicked() {
757 : 2 : QString store = selectFolder();
758 [ + + ]: 2 : if (!store.isEmpty()) {
759 : 1 : ui->storePath->setText(store);
760 : : }
761 : 2 : }
762 : :
763 : : // index 0 is "No clipboard".
764 : 182 : void ConfigDialog::on_comboBoxClipboard_activated(int index) {
765 : 182 : bool state = index > 0;
766 : :
767 : 182 : ui->checkBoxSelection->setEnabled(state);
768 : 182 : ui->checkBoxHidePassword->setEnabled(state);
769 : 182 : updateSecretFieldsEnabled();
770 : 182 : ui->checkBoxHideContent->setEnabled(state);
771 : 182 : ui->labelAutoclear->setEnabled(state);
772 : 182 : ui->spinBoxAutoclearSeconds->setEnabled(state);
773 : 182 : ui->labelSeconds->setEnabled(state);
774 : 182 : ui->checkBoxRestoreClipboard->setEnabled(state);
775 : 182 : }
776 : :
777 : 398 : void ConfigDialog::updateSecretFieldsEnabled() {
778 : : // Each control only while the one it depends on is on: other secrets and
779 : : // every field only while passwords are hidden; the secret words only while
780 : : // other secrets are, and not when every field is hidden anyway.
781 [ + + + + ]: 675 : const bool hiding = ui->checkBoxHidePassword->isEnabled() &&
782 : 277 : ui->checkBoxHidePassword->isChecked();
783 : 398 : ui->checkBoxHideAllFields->setEnabled(hiding);
784 [ + + + + ]: 398 : const bool all = hiding && ui->checkBoxHideAllFields->isChecked();
785 : 398 : ui->checkBoxHideSecretFields->setEnabled(hiding && !all);
786 : : const bool words =
787 [ + + + + ]: 398 : hiding && !all && ui->checkBoxHideSecretFields->isChecked();
788 : 398 : ui->lineEditSecretFields->setEnabled(words);
789 : 398 : ui->buttonDefaultSecretFields->setEnabled(words);
790 : 398 : }
791 : :
792 : 92 : void ConfigDialog::useSelection(bool useSelection) {
793 : 92 : ui->checkBoxSelection->setChecked(useSelection);
794 : 92 : on_checkBoxSelection_clicked();
795 : 92 : }
796 : :
797 : 92 : void ConfigDialog::on_checkBoxSelection_clicked() {
798 : 92 : on_comboBoxClipboard_activated(ui->comboBoxClipboard->currentIndex());
799 : 92 : }
800 : :
801 : 90 : void ConfigDialog::setProfiles(Profiles profiles,
802 : : const QString ¤tProfile) {
803 : : // remove weird "" key value pairs
804 : 90 : profiles.remove(QString());
805 : :
806 : 90 : m_entries.clear();
807 : 90 : ui->profileList->clear();
808 : : int currentRow = -1;
809 [ + + ]: 129 : for (auto i = profiles.cbegin(); i != profiles.cend(); ++i) {
810 [ + + ]: 39 : if (i.key() == currentProfile) {
811 : 18 : currentRow = static_cast<int>(m_entries.size());
812 : : }
813 : 39 : m_entries.append({i.key(), i.value(), i.key()});
814 : 39 : ui->profileList->addItem(i.key());
815 : : }
816 : 90 : ui->profileList->setCurrentRow(currentRow);
817 [ + + ]: 90 : if (currentRow < 0) {
818 : 72 : loadProfileForm(-1);
819 : : }
820 : 90 : validate();
821 : 129 : }
822 : :
823 : 295 : auto ConfigDialog::currentEntry() -> ProfileEntry * {
824 [ + + + - ]: 295 : if (m_currentEntry < 0 || m_currentEntry >= m_entries.size()) {
825 : : return nullptr;
826 : : }
827 : 151 : return &m_entries[m_currentEntry];
828 : : }
829 : :
830 : : // The Git boxes show the profile's own flags, else the global ones; only a
831 : : // click on a box makes the value the profile's own.
832 : 109 : void ConfigDialog::loadProfileForm(int row) {
833 [ + + - + ]: 109 : m_currentEntry = (row >= 0 && row < m_entries.size()) ? row : -1;
834 : 109 : const ProfileEntry *entry = currentEntry();
835 : 109 : m_loadingForm = true;
836 : 109 : ui->profileForm->setEnabled(entry != nullptr);
837 : 109 : ui->deleteButton->setEnabled(entry != nullptr);
838 [ + + + + : 255 : ui->profileName->setText(entry ? entry->name : QString());
+ ]
839 [ + + + + : 255 : ui->profilePath->setText(entry ? entry->profile.path : QString());
+ ]
840 [ + + + ]: 146 : ui->profileSigningKey->setText(entry ? entry->profile.signingKey : QString());
841 : : const bool useGit =
842 [ + + + + ]: 146 : entry && entry->profile.useGit.value_or(ui->checkBoxUseGit->isChecked());
843 : 109 : ui->profileUseGit->setChecked(useGit);
844 : 109 : ui->profileAutoPush->setChecked(
845 [ + + + + ]: 146 : entry &&
846 : 37 : entry->profile.autoPush.value_or(ui->checkBoxAutoPush->isChecked()));
847 : 109 : ui->profileAutoPull->setChecked(
848 [ + + + + ]: 146 : entry &&
849 : 37 : entry->profile.autoPull.value_or(ui->checkBoxAutoPull->isChecked()));
850 : 109 : ui->profileAutoPush->setEnabled(useGit);
851 : 109 : ui->profileAutoPull->setEnabled(useGit);
852 : 109 : m_loadingForm = false;
853 : 109 : updateProfileStatus();
854 : 109 : validate();
855 : 109 : }
856 : :
857 : 70 : auto ConfigDialog::getProfiles() -> Profiles {
858 : 70 : Profiles profiles;
859 [ + + ]: 108 : for (const ProfileEntry &entry : std::as_const(m_entries)) {
860 : 38 : profiles.insert(entry.name, entry.profile);
861 : : }
862 : 70 : return profiles;
863 : : }
864 : :
865 : 6 : auto ConfigDialog::ensureProfileFolder(const QString &path) -> bool {
866 [ + + + ]: 6 : if (QDir(path).exists()) {
867 : : return true;
868 : : }
869 : 4 : if (QMessageBox::question(
870 : 2 : this, tr("Create profile directory?"),
871 : 4 : tr("Would you like to create a password store at %1?").arg(path),
872 : : QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) {
873 : : return false;
874 : : }
875 [ + - ]: 1 : if (!Util::mkpathForStore(path)) {
876 : 1 : PlainMessageBox::warning(
877 : 1 : this, tr("Error"),
878 : 2 : tr("Could not create profile directory: %1").arg(path));
879 : 1 : return false;
880 : : }
881 : : return true;
882 : : }
883 : :
884 : 3 : void ConfigDialog::initialiseProfileStore(const QString &name,
885 : : const Profile &profile,
886 : : const QString &path) {
887 : : // Only the recipients come through the active backend. passStore is the
888 : : // profile, or getGpgIdPath() would pre-tick the active store's recipients;
889 : : // signing keys are per profile, exactly as the profile switch applies them
890 : : // (MainWindow::on_profileBox_currentTextChanged).
891 : 3 : const AppSettings settings = QtPassSettings::load();
892 : 3 : AppSettings profileSettings = settings;
893 : 3 : profileSettings.passStore = Util::normalizeFolderPath(path);
894 : 3 : profileSettings.passSigningKey = profile.signingKey;
895 : : // needsInit() found no .gpg-id: nothing to preselect, and none that
896 : : // appears meanwhile is taken (it would be read under the active
897 : : // profile's signing policy).
898 : : UsersDialog usersDialog(QtPassSettings::getPass(), profileSettings,
899 : : profileSettings.passStore,
900 [ + + + ]: 6 : UsersDialog::Preselect::Nothing, this);
901 : : usersDialog.setInitOnAccept(false);
902 : 6 : usersDialog.setWindowTitle(tr("Select recipients for %1").arg(name));
903 [ + + + ]: 3 : if (usersDialog.exec() != QDialog::Accepted) {
904 : : return;
905 : : }
906 : 2 : QString note;
907 : 2 : const bool ok = ProfileInit::initialise(
908 : 2 : path, usersDialog.selectedUsers(), profileSettings,
909 : : profile.useGit.value_or(settings.useGit), ¬e);
910 [ + + ]: 2 : if (!ok) {
911 : : // ProfileInit writes .gpg-id before it signs and commits. Left behind,
912 : : // needsInit() would find a store that looks finished, and saving the
913 : : // profile again would never retry; the wizard takes it back the same
914 : : // way.
915 : 2 : QFile::remove(QDir(path).filePath(QStringLiteral(".gpg-id")));
916 : 2 : QFile::remove(QDir(path).filePath(QStringLiteral(".gpg-id.sig")));
917 : 1 : PlainMessageBox::warning(
918 : 2 : this, tr("Could not initialise profile %1").arg(name), note);
919 [ + - ]: 1 : } else if (!note.isEmpty()) {
920 : 2 : PlainMessageBox::show(this, QMessageBox::Information,
921 : 2 : tr("Profile %1").arg(name), note);
922 : : }
923 : 3 : }
924 : :
925 : 30 : void ConfigDialog::initializeNewProfiles(const Profiles &existingProfiles) {
926 : 30 : const Profiles newProfiles = getProfiles();
927 : : // QMap iterates in name order.
928 [ + + ]: 73 : for (auto it = newProfiles.cbegin(); it != newProfiles.cend(); ++it) {
929 : 13 : const QString path = QDir::cleanPath(it.value().path);
930 : : // A store that was already a profile when the dialog opened, under this
931 : : // or another name: a rename is not a new store.
932 : 13 : const bool known = std::any_of(
933 : : existingProfiles.cbegin(), existingProfiles.cend(),
934 [ + + ]: 9 : [&it](const Profile &old) { return old.path == it.value().path; });
935 : : // needsInit is false for a missing folder, so it is created first.
936 [ + + + + : 13 : if (!known && ensureProfileFolder(path) && ProfileInit::needsInit(path)) {
+ + + + ]
937 : 3 : initialiseProfileStore(it.key(), it.value(), path);
938 : : }
939 : : }
940 : 30 : }
941 : :
942 : 7 : void ConfigDialog::on_addButton_clicked() {
943 : 7 : ProfileEntry entry;
944 : 7 : entry.name = tr("New profile");
945 : 7 : entry.profile.path = ui->storePath->text();
946 : 7 : m_entries.append(entry);
947 : 7 : ui->profileList->addItem(entry.name);
948 : 7 : ui->profileList->setCurrentRow(static_cast<int>(m_entries.size()) - 1);
949 : 7 : ui->profileName->setFocus();
950 : 7 : ui->profileName->selectAll();
951 : 7 : validate();
952 : 7 : }
953 : :
954 : 1 : void ConfigDialog::on_profilePathBrowse_clicked() {
955 : 1 : const QString dir = selectFolder();
956 [ + - ]: 1 : if (!dir.isEmpty()) {
957 : 1 : ui->profilePath->setText(dir);
958 : 1 : onProfilePathEdited(dir);
959 : : }
960 : 1 : }
961 : :
962 : 4 : void ConfigDialog::on_deleteButton_clicked() {
963 : 4 : const int row = ui->profileList->currentRow();
964 [ + + - + ]: 4 : if (row < 0 || row >= m_entries.size()) {
965 : 3 : QMessageBox::warning(this, tr("No profile selected"),
966 : 1 : tr("No profile selected to delete"));
967 : 1 : return;
968 : : }
969 : 3 : m_entries.removeAt(row);
970 : 3 : delete ui->profileList->takeItem(row);
971 : : // The list moved its current row while the row was being removed, so
972 : : // currentRowChanged carried the old numbering: the form named the profile
973 : : // after the one now highlighted, and edits (a signing key) went there.
974 : : // Load it from the row the list shows now.
975 : 3 : loadProfileForm(ui->profileList->currentRow());
976 : : }
977 : :
978 : 0 : void ConfigDialog::criticalMessage(const QString &title, const QString &text) {
979 : 0 : QMessageBox::critical(this, title, text, QMessageBox::Ok, QMessageBox::Ok);
980 : 0 : }
981 : :
982 : : // Looks on Util's PATH (with the macOS and Windows additions) without a
983 : : // subprocess; a hit is stored for the QR display, a miss leaves the setting.
984 : 90 : auto ConfigDialog::isQrencodeAvailable(const QString &configuredPath) -> bool {
985 : : #ifdef Q_OS_WIN
986 : : Q_UNUSED(configuredPath);
987 : : return false;
988 : : #else
989 [ + + ]: 90 : if (!configuredPath.isEmpty()) {
990 : 8 : const QFileInfo configured(configuredPath);
991 [ + + + + : 8 : if (configured.isFile() && configured.isExecutable()) {
+ - ]
992 : : return true;
993 : : }
994 : 8 : }
995 : 178 : const QString found = Util::findBinaryInPath(QStringLiteral("qrencode"));
996 [ + + ]: 89 : if (found.isEmpty()) {
997 : : return false;
998 : : }
999 : 1 : QtPassSettings::setQrencodeExecutable(found);
1000 : : return true;
1001 : : #endif
1002 : : }
1003 : :
1004 : 90 : void ConfigDialog::useTrayIcon(bool useSystray) {
1005 [ - + ]: 90 : if (QSystemTrayIcon::isSystemTrayAvailable()) {
1006 : 0 : ui->checkBoxUseTrayIcon->setChecked(useSystray);
1007 : 0 : ui->checkBoxHideOnClose->setEnabled(useSystray);
1008 : 0 : ui->checkBoxStartMinimized->setEnabled(useSystray);
1009 : :
1010 [ # # ]: 0 : if (!useSystray) {
1011 : 0 : ui->checkBoxHideOnClose->setChecked(false);
1012 : 0 : ui->checkBoxStartMinimized->setChecked(false);
1013 : : }
1014 : : }
1015 : 90 : }
1016 : :
1017 : 3 : void ConfigDialog::on_checkBoxUseTrayIcon_clicked() {
1018 : 3 : bool state = ui->checkBoxUseTrayIcon->isChecked();
1019 : 3 : ui->checkBoxHideOnClose->setEnabled(state);
1020 : 3 : ui->checkBoxStartMinimized->setEnabled(state);
1021 : 3 : }
1022 : :
1023 : 101 : void ConfigDialog::useGit(bool useGit) {
1024 : : // Without a git path the checkbox is disabled; ticked as well, it could
1025 : : // not be unticked.
1026 [ + + ]: 202 : if (ui->gitPath->text().isEmpty()) {
1027 : : useGit = false;
1028 : : }
1029 : 101 : ui->checkBoxUseGit->setChecked(useGit);
1030 : 101 : on_checkBoxUseGit_clicked();
1031 : 101 : }
1032 : :
1033 : 92 : void ConfigDialog::useOtp(bool useOtp) {
1034 : 92 : ui->checkBoxUseOtp->setChecked(useOtp);
1035 : 92 : }
1036 : :
1037 : 92 : void ConfigDialog::useGrepSearch(bool useGrepSearch) {
1038 : 92 : ui->checkBoxUseGrepSearch->setChecked(useGrepSearch);
1039 : 92 : }
1040 : :
1041 : 92 : void ConfigDialog::useQrencode(bool useQrencode) {
1042 : 92 : ui->checkBoxUseQrencode->setChecked(useQrencode);
1043 : 92 : }
1044 : :
1045 : 214 : void ConfigDialog::on_checkBoxUseGit_clicked() {
1046 : 214 : ui->checkBoxAddGPGId->setEnabled(ui->checkBoxUseGit->isChecked());
1047 : 214 : ui->checkBoxAutoPull->setEnabled(ui->checkBoxUseGit->isChecked());
1048 : 214 : ui->checkBoxAutoPush->setEnabled(ui->checkBoxUseGit->isChecked());
1049 : : // pass always stops git at the store's parent itself, so the choice only
1050 : : // means something in native mode.
1051 : 214 : ui->checkBoxGitUseParentRepository->setEnabled(
1052 [ + + + + ]: 214 : ui->checkBoxUseGit->isChecked() && !ui->radioButtonPass->isChecked());
1053 : 214 : }
1054 : :
1055 : 2 : void ConfigDialog::on_toolButtonPwgen_clicked() {
1056 : 2 : QString pwgen = selectExecutable();
1057 [ + + ]: 2 : if (!pwgen.isEmpty()) {
1058 : 1 : setPwgenPath(pwgen);
1059 : : }
1060 : 2 : }
1061 : :
1062 : 98 : void ConfigDialog::setPwgenPath(const QString &pwgen) {
1063 : 98 : ui->pwgenPath->setText(pwgen);
1064 : : // setText() is silent when the text did not change.
1065 : 98 : updatePwgenCheckbox();
1066 : 98 : }
1067 : :
1068 : : // Every way to a pwgen path ends here: loading the settings (where the backend
1069 : : // step runs while the path is still empty), Autodetect, the file dialog and
1070 : : // typing in the field.
1071 : 223 : void ConfigDialog::updatePwgenCheckbox() {
1072 : 223 : const bool hasPwgen = !ui->pwgenPath->text().isEmpty();
1073 [ + + ]: 223 : if (!hasPwgen) {
1074 : 161 : ui->checkBoxUsePwgen->setChecked(false);
1075 : : }
1076 [ + + + + ]: 450 : ui->checkBoxUsePwgen->setEnabled(hasPwgen &&
1077 : 62 : !ui->radioButtonPass->isChecked());
1078 : 223 : on_checkBoxUsePwgen_clicked();
1079 : 223 : }
1080 : :
1081 : 319 : void ConfigDialog::on_checkBoxUsePwgen_clicked() {
1082 [ + + ]: 319 : if (ui->radioButtonPass->isChecked())
1083 : : return;
1084 : 311 : bool usePwgen = ui->checkBoxUsePwgen->isChecked();
1085 : 311 : ui->checkBoxAvoidCapitals->setEnabled(usePwgen);
1086 : 311 : ui->checkBoxAvoidNumbers->setEnabled(usePwgen);
1087 : 311 : ui->checkBoxLessRandom->setEnabled(usePwgen);
1088 : 311 : ui->checkBoxUseSymbols->setEnabled(usePwgen);
1089 : : // Only the custom set is editable; a builtin one is saved as it is.
1090 : 311 : ui->lineEditPasswordChars->setEnabled(
1091 [ + + + + ]: 311 : !usePwgen && ui->passwordCharTemplateSelector->currentIndex() ==
1092 : : PasswordConfiguration::CUSTOM);
1093 : 311 : ui->labelPasswordChars->setEnabled(!usePwgen);
1094 : 311 : ui->passwordCharTemplateSelector->setEnabled(!usePwgen);
1095 : : }
1096 : :
1097 : 95 : void ConfigDialog::usePwgen(bool usePwgen) {
1098 [ + + ]: 190 : if (ui->pwgenPath->text().isEmpty()) {
1099 : : usePwgen = false;
1100 : : }
1101 : 95 : ui->checkBoxUsePwgen->setChecked(usePwgen);
1102 : 95 : on_checkBoxUsePwgen_clicked();
1103 : 95 : }
1104 : :
1105 : 94 : void ConfigDialog::setPasswordConfiguration(
1106 : : const PasswordConfiguration &config) {
1107 : : // Retain the custom charset so it survives even when a builtin set is the
1108 : : // active selection (the line edit then shows the builtin's characters).
1109 : 94 : m_customPasswordChars = config.Characters[PasswordConfiguration::CUSTOM];
1110 : 94 : ui->spinBoxPasswordLength->setValue(config.length);
1111 : 94 : ui->passwordCharTemplateSelector->setCurrentIndex(config.selected);
1112 [ + + ]: 94 : if (config.selected != PasswordConfiguration::CUSTOM) {
1113 : 92 : ui->lineEditPasswordChars->setEnabled(false);
1114 : : }
1115 : 94 : ui->lineEditPasswordChars->setText(config.Characters[config.selected]);
1116 : 94 : }
1117 : :
1118 : 34 : auto ConfigDialog::getPasswordConfiguration() -> PasswordConfiguration {
1119 : 34 : PasswordConfiguration config;
1120 : 34 : config.length = ui->spinBoxPasswordLength->value();
1121 : 34 : config.selected = static_cast<PasswordConfiguration::CharacterSet>(
1122 : 34 : ui->passwordCharTemplateSelector->currentIndex());
1123 : : // For a builtin selection the line edit shows the builtin's characters;
1124 : : // reading it would overwrite the saved custom charset.
1125 [ + + ]: 34 : if (config.selected == PasswordConfiguration::CUSTOM) {
1126 : : config.Characters[PasswordConfiguration::CUSTOM] =
1127 : 4 : ui->lineEditPasswordChars->text();
1128 : : } else {
1129 : 32 : config.Characters[PasswordConfiguration::CUSTOM] = m_customPasswordChars;
1130 : : }
1131 : 34 : return config;
1132 : : }
1133 : :
1134 : 4 : void ConfigDialog::on_passwordCharTemplateSelector_activated(int index) {
1135 : 4 : ui->lineEditPasswordChars->setText(
1136 : 4 : QtPassSettings::getPasswordConfiguration().Characters[index]);
1137 [ + + ]: 4 : if (index == PasswordConfiguration::CUSTOM) {
1138 : 2 : ui->lineEditPasswordChars->setEnabled(true);
1139 : : } else {
1140 : 2 : ui->lineEditPasswordChars->setEnabled(false);
1141 : : }
1142 : 4 : }
1143 : :
1144 : 34 : void ConfigDialog::onProfileSelected(int row) { loadProfileForm(row); }
1145 : :
1146 : 22 : void ConfigDialog::onProfileNameEdited(const QString &name) {
1147 : 22 : ProfileEntry *entry = currentEntry();
1148 [ + - + - ]: 22 : if (m_loadingForm || entry == nullptr) {
1149 : : return;
1150 : : }
1151 : 22 : entry->name = name;
1152 [ + - ]: 22 : if (QListWidgetItem *item = ui->profileList->item(m_currentEntry)) {
1153 : 22 : item->setText(name);
1154 : : }
1155 : 22 : validate();
1156 : 22 : updateProfileStatus();
1157 : : }
1158 : :
1159 : 9 : void ConfigDialog::onProfilePathEdited(const QString &path) {
1160 : 9 : ProfileEntry *entry = currentEntry();
1161 [ + - + - ]: 9 : if (m_loadingForm || entry == nullptr) {
1162 : : return;
1163 : : }
1164 : 9 : entry->profile.path = path;
1165 : 9 : validate();
1166 : 9 : updateProfileStatus();
1167 : : }
1168 : :
1169 : 14 : void ConfigDialog::onProfileSigningKeyEdited(const QString &key) {
1170 : 14 : ProfileEntry *entry = currentEntry();
1171 [ + - + - ]: 14 : if (m_loadingForm || entry == nullptr) {
1172 : : return;
1173 : : }
1174 : 14 : entry->profile.signingKey = key;
1175 : 14 : validate();
1176 : : }
1177 : :
1178 : : // A Git box was clicked: the profile now has its own flags.
1179 : 1 : void ConfigDialog::onProfileGitToggled() {
1180 : 1 : ProfileEntry *entry = currentEntry();
1181 [ + - + - ]: 1 : if (m_loadingForm || entry == nullptr) {
1182 : : return;
1183 : : }
1184 : 1 : entry->profile.useGit = ui->profileUseGit->isChecked();
1185 : 1 : entry->profile.autoPush = ui->profileAutoPush->isChecked();
1186 : 1 : entry->profile.autoPull = ui->profileAutoPull->isChecked();
1187 : 1 : ui->profileAutoPush->setEnabled(ui->profileUseGit->isChecked());
1188 : 1 : ui->profileAutoPull->setEnabled(ui->profileUseGit->isChecked());
1189 : : }
1190 : :
1191 : 140 : void ConfigDialog::updateProfileStatus() {
1192 : 140 : const ProfileEntry *entry = currentEntry();
1193 [ + + ]: 140 : if (entry == nullptr) {
1194 : 72 : ui->statusLabel->setText(QString());
1195 : 72 : return;
1196 : : }
1197 : :
1198 : 68 : QString statusMessage;
1199 [ + + + + ]: 68 : if (!entry->name.isEmpty() && !entry->profile.path.isEmpty()) {
1200 : 65 : const QString path = QDir::cleanPath(entry->profile.path);
1201 [ + + + + ]: 65 : if (!QDir(path).exists()) {
1202 : 80 : statusMessage = tr("New profile: %1 at %2").arg(entry->name, path);
1203 : : } else {
1204 : 50 : statusMessage = tr("Profile: %1 at %2").arg(entry->name, path);
1205 : : }
1206 : : } else {
1207 : 3 : statusMessage = tr("Fill in all required fields");
1208 : : }
1209 : :
1210 : 68 : ui->statusLabel->setText(statusMessage);
1211 : : }
|