Branch data Line data Source code
1 : : // SPDX-FileCopyrightText: 2018 Anne Jan Brouwer
2 : : // SPDX-License-Identifier: GPL-3.0-or-later
3 : : /**
4 : : * @file
5 : : * @brief Implements QtPass; init() holds the settings migrations run on every
6 : : * start-up and showTextAsQRCode() drives the qrencode process.
7 : : */
8 : : #include "qtpass.h"
9 : : #include "executor.h"
10 : : #include "imitatepass.h"
11 : : #include "qtpasssettings.h"
12 : : #include "settingsconstants.h"
13 : : #include "util.h"
14 : : #include <QApplication>
15 : : #include <QDialog>
16 : : #include <QLabel>
17 : : #include <QPixmap>
18 : : #include <QVBoxLayout>
19 : :
20 : : #include "qtpasslogging.h"
21 : :
22 : 222 : QtPass::QtPass(QObject *parent) : QObject(parent), m_clipboard(this) {
23 : : // Both implementations are wired up: the active one can change through the
24 : : // configuration dialog, and reconnecting on every switch is not worth it.
25 : 222 : connectPassSignalHandlers(QtPassSettings::getRealPass());
26 : 222 : connectPassSignalHandlers(QtPassSettings::getImitatePass());
27 : 222 : }
28 : :
29 : 428 : QtPass::~QtPass() = default;
30 : :
31 : 209 : void QtPass::init() {
32 : 209 : QString passStore = QtPassSettings::getPassStore(Util::findPasswordStore());
33 : 209 : QtPassSettings::setPassStore(passStore);
34 : :
35 : 209 : QtPassSettings::initExecutables();
36 : :
37 : 418 : QString version = QtPassSettings::getVersion();
38 : :
39 : : // Config updates
40 [ + + ]: 209 : if (version.isEmpty()) {
41 : 4 : qCDebug(lcQtPass) << "assuming fresh install";
42 : 4 : AppSettings s = QtPassSettings::load();
43 [ + - ]: 4 : if (s.autoclearSeconds < 5)
44 : 4 : s.autoclearSeconds = 10;
45 [ + - ]: 4 : if (s.autoclearPanelSeconds < 5)
46 : 4 : s.autoclearPanelSeconds = 10;
47 : 4 : s.usePwgen = !s.pwgenExecutable.isEmpty();
48 : 4 : s.passTemplate = QStringLiteral("login\nurl\nOTP");
49 : 4 : QtPassSettings::save(s);
50 : : // A fresh profile already has the new useOtp default, so record the
51 : : // migration as done. Without this, turning OTP off during the first session
52 : : // would be undone by the migration branch on the next launch.
53 : 8 : QtPassSettings::getInstance()->setValue(
54 : 8 : SettingsConstants::otpMigratedToNative, true);
55 : : // Nothing chosen yet: the first-run wizard may suggest the backend and
56 : : // Git from what it finds, until it is finished.
57 : 8 : QtPassSettings::getInstance()->setValue(SettingsConstants::firstRunPending,
58 : 8 : true);
59 : 4 : } else {
60 : 205 : AppSettings s = QtPassSettings::load();
61 : : bool changed = false;
62 [ + + ]: 205 : if (s.passTemplate.isEmpty()) {
63 : 1 : s.passTemplate = QStringLiteral("login\nurl\nOTP");
64 : : changed = true;
65 : : }
66 : : // `useOtp` used to gate the Unix-only pass-otp extension and now gates
67 : : // built-in TOTP, so an existing profile's stored false is stale rather than
68 : : // a preference. The new default of true never reaches these users: the
69 : : // fresh-install branch above already persisted the key, and the serializer
70 : : // writes it unconditionally. Enable it once, remembered by its own key so a
71 : : // deliberate opt-out is not overridden on the next launch.
72 : 205 : if (!QtPassSettings::getInstance()
73 : 410 : ->value(SettingsConstants::otpMigratedToNative, false)
74 : 205 : .toBool()) {
75 : 1 : s.useOtp = true;
76 : 2 : QtPassSettings::getInstance()->setValue(
77 : 2 : SettingsConstants::otpMigratedToNative, true);
78 : : changed = true;
79 : : }
80 [ - + ]: 204 : if (changed) {
81 : 1 : QtPassSettings::save(s);
82 : : }
83 : 205 : }
84 : :
85 : 418 : QtPassSettings::setVersion(VERSION);
86 : 209 : }
87 : :
88 : 444 : void QtPass::connectPassSignalHandlers(Pass *pass) {
89 : 444 : connect(pass, &Pass::processErrorExit, this, &QtPass::processErrorExit);
90 : 444 : connect(pass, &Pass::finishedGitInit, this, &QtPass::passStoreChanged);
91 : 444 : connect(pass, &Pass::finishedGitPull, this, &QtPass::processFinished);
92 : 444 : connect(pass, &Pass::finishedGitPush, this, &QtPass::processFinished);
93 : 444 : connect(pass, &Pass::finishedInsert, this, &QtPass::finishedInsert);
94 : 444 : connect(pass, &Pass::finishedRemove, this, &QtPass::passStoreChanged);
95 : 444 : connect(pass, &Pass::finishedInit, this, &QtPass::passStoreChanged);
96 : 444 : connect(pass, &Pass::finishedMove, this, &QtPass::passStoreChanged);
97 : 444 : connect(pass, &Pass::finishedCopy, this, &QtPass::passStoreChanged);
98 : 444 : connect(pass, &Pass::finishedGenerateGPGKeys, this,
99 : 444 : &QtPass::onKeyGenerationComplete);
100 : 444 : }
101 : :
102 : 86 : void QtPass::reportError(int exitCode, const QString &error) {
103 [ + + ]: 86 : if (error.isEmpty()) {
104 : 40 : return;
105 : : }
106 : : const QString colour =
107 [ + + + + : 53 : exitCode == 0 ? QStringLiteral("darkgray") : QStringLiteral("red");
+ ]
108 : 92 : emit outputReady(formatOutput(
109 : 92 : error, QStringLiteral("<span style=\"color: %1;\">").arg(colour),
110 : 46 : QStringLiteral("</span><br />")));
111 : : }
112 : :
113 : 43 : void QtPass::processErrorExit(int exitCode, const QString &error) {
114 : 43 : reportError(exitCode, error);
115 : 43 : emit operationFinished();
116 : 43 : }
117 : :
118 : 43 : void QtPass::processFinished(const QString &output, const QString &errout) {
119 : : // A silent command (git push with nothing to push, say) has nothing to
120 : : // show; re-setting the browser's HTML for it would only reset its scroll.
121 [ + + ]: 43 : if (!output.isEmpty()) {
122 : 8 : emit outputReady(formatOutput(output));
123 : : }
124 : 43 : reportError(0, errout);
125 : 43 : emit operationFinished();
126 : 43 : }
127 : :
128 : 7 : void QtPass::passStoreChanged(const QString &output, const QString &errout) {
129 : 7 : processFinished(output, errout);
130 : 7 : doGitPush();
131 : 7 : }
132 : :
133 : 13 : void QtPass::finishedInsert(const QString &output, const QString &errout) {
134 : 13 : processFinished(output, errout);
135 : 13 : doGitPush();
136 : 13 : emit entryInserted();
137 : 13 : }
138 : :
139 : 2 : void QtPass::onKeyGenerationComplete(const QString &output,
140 : : const QString &errout) {
141 : : // The KeygenDialog listens to the same signal and closes itself.
142 : 2 : emit statusMessage(tr("GPG key pair generated successfully"), 10000);
143 : 2 : processFinished(output, errout);
144 : 2 : }
145 : :
146 : 94 : auto QtPass::formatOutput(QString output, const QString &prefix,
147 : : const QString &postfix, bool links) -> QString {
148 : : // White space is kept below, so a "\r\n" must be one line end, and a lone
149 : : // "\r" stays a space inside its line, as before: as a block of its own it
150 : : // would slip past the display cap, which counts "\n" only. So do U+2029
151 : : // and the frame marks, which Qt also starts a block at.
152 : 188 : output.replace(QStringLiteral("\r\n"), QStringLiteral("\n"));
153 : 94 : output = Util::withoutOtherBlockBreaks(output);
154 : : // Kept white space can hang Qt's layout on a run of 65536 spaces.
155 : 94 : output = Util::boundSpaceRuns(output);
156 : : // Escapes and links only launchable http(s) URLs; anything else stays
157 : : // plain text (the text browser opens external links on click).
158 [ + + ]: 94 : output = links ? Util::linkifyUrls(output) : output.toHtmlEscaped();
159 : 188 : output.replace(QStringLiteral("\n"), "<br />");
160 : : // As stored: rich text otherwise shows, and Select All + Copy hand back,
161 : : // "a b" as "a b", drops indentation and turns a tab into a space (#210).
162 : : // Long lines still wrap.
163 : 188 : return prefix + QStringLiteral("<span style=\"white-space: pre-wrap;\">") +
164 : 188 : output + QStringLiteral("</span>") + postfix;
165 : : }
166 : :
167 : 20 : void QtPass::doGitPush() {
168 [ + + + + ]: 20 : if (!QtPassSettings::isAutoPush()) {
169 : : return;
170 : : }
171 : : // After a re-encryption that did not finish: the push would publish the
172 : : // recipients change with entries still for the previous recipients.
173 : 16 : if (Pass *pass = QtPassSettings::getPass();
174 [ + - + + ]: 16 : pass != nullptr && pass->autoPushHeld()) {
175 : 2 : emit statusMessage(tr("Not pushing automatically: the last "
176 : : "re-encryption did not finish. Re-encrypt again, "
177 : : "or push by hand."),
178 : : 10000);
179 : 2 : return;
180 : : }
181 : 14 : emit pushRequested();
182 : : }
183 : :
184 : 6 : void QtPass::showTextAsQRCode(const QString &text) {
185 : 6 : const AppSettings s = QtPassSettings::load();
186 : : const QString qrExe = s.qrencodeExecutable.isEmpty()
187 [ - + ]: 6 : ? QStringLiteral("/usr/bin/qrencode")
188 : : : s.qrencodeExecutable;
189 : 6 : QProcess qrencode;
190 : 6 : Executor::detachFromTerminal(qrencode);
191 : 18 : qrencode.start(qrExe, QStringList() << "-o-"
192 : 12 : << "-tPNG");
193 : : // A missing or non-executable binary never "finishes": exitStatus() stays
194 : : // NormalExit and exitCode() 0, which used to fall through to an empty popup.
195 [ + + + ]: 6 : if (!qrencode.waitForStarted()) {
196 : 2 : emit statusMessage(tr("Could not start qrencode: %1").arg(qrExe), 2000);
197 : 1 : return;
198 : : }
199 : 5 : qrencode.write(text.toUtf8());
200 : 5 : qrencode.closeWriteChannel();
201 : : // A hung qrencode also leaves exitStatus()/exitCode() at their defaults.
202 [ + - + ]: 5 : if (!qrencode.waitForFinished()) {
203 : 0 : qrencode.kill();
204 : 0 : emit statusMessage(tr("qrencode did not finish in time"), 2000);
205 : 0 : return;
206 : : }
207 : 5 : QByteArray output(qrencode.readAllStandardOutput());
208 : :
209 : 5 : const bool crashed = qrencode.exitStatus() != QProcess::NormalExit;
210 : : // exitCode() is meaningless after a crash, so only consult it otherwise.
211 [ + + + + : 5 : const bool failed = !crashed && qrencode.exitCode() != 0;
+ ]
212 [ + + ]: 3 : if (crashed || failed) {
213 : 6 : QString error(qrencode.readAllStandardError());
214 [ + + ]: 3 : if (error.trimmed().isEmpty()) {
215 : : error = crashed
216 [ + + + ]: 4 : ? tr("qrencode crashed")
217 : 3 : : tr("qrencode exited with code %1").arg(qrencode.exitCode());
218 : : }
219 : 3 : emit statusMessage(error, 2000);
220 : : } else {
221 : 2 : QPixmap image;
222 : 2 : image.loadFromData(output, "PNG");
223 : 2 : m_qrPopup = createQRCodePopup(image);
224 : 2 : m_qrPopup->exec();
225 : 2 : }
226 : 12 : }
227 : :
228 : 69 : void QtPass::closeQRCode() {
229 [ + + ]: 69 : if (m_qrPopup) {
230 : 1 : m_qrPopup->close();
231 : : }
232 : 69 : }
233 : :
234 : 3 : QDialog *QtPass::createQRCodePopup(const QPixmap &image) {
235 : 3 : auto *popup = new QDialog(nullptr, Qt::Popup | Qt::FramelessWindowHint);
236 : 3 : popup->setAttribute(Qt::WA_DeleteOnClose);
237 : 3 : auto *layout = new QVBoxLayout;
238 : 3 : auto *popupLabel = new QLabel();
239 : 3 : layout->addWidget(popupLabel);
240 : 3 : popupLabel->setPixmap(image);
241 : 3 : popupLabel->setScaledContents(true);
242 : 3 : popupLabel->show();
243 : 3 : popup->setLayout(layout);
244 : 3 : popup->move(QCursor::pos());
245 : 3 : return popup;
246 : : }
|