summaryrefslogtreecommitdiffstats
path: root/pass.cpp
blob: a4cf7862f15862591f1206fe2d1dbfbad40cda3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/******************************************************************************
 *  Copyright (C) 2017 by Lukas Fürmetz <fuermetz@mailbox.org>                *
 *                                                                            *
 *  This library is free software; you can redistribute it and/or modify      *
 *  it under the terms of the GNU General Public License as published         *
 *  by the Free Software Foundation; either version 3 of the License or (at   *
 *  your option) any later version.                                           *
 *                                                                            *
 *  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         *
 *  Library General Public License for more details.                          *
 *                                                                            *
 *  You should have received a copy of the GNU General Public License         *
 *  along with this library; see the file LICENSE.                            *
 *  If not, see <http://www.gnu.org/licenses/>.                               *
 *****************************************************************************/
#include <KSharedConfig>
#include <KLocalizedString>
#include <KNotification>

#include <QAction>
#include <QDirIterator>
#include <QProcess>
#include <QRegularExpression>
#include <QTimer>
#include <QMessageBox>
#include <QClipboard>
#include <QDebug>
#include <QApplication>

#include <cstdlib>

#include "pass.h"
#include "config.h"

using namespace std;


Pass::Pass(QObject *parent, const QVariantList &args)
    : Plasma::AbstractRunner(parent, args)
{
    // General runner configuration
    setObjectName(QStringLiteral("Pass"));
    setSpeed(AbstractRunner::NormalSpeed);
    setPriority(HighestPriority);
}

Pass::~Pass() = default;

void Pass::reloadConfiguration()
{
    clearActions();
    orderedActions.clear();

    KConfigGroup cfg = config();
    cfg.config()->reparseConfiguration(); // Just to be sure
    this->showActions = cfg.readEntry(Config::showActions, false);

    if (showActions) {
        const auto configActions = cfg.group(Config::Group::Actions);

        // Create actions for every additional field
        const auto configActionsList = configActions.groupList();
        for (const auto &name: configActionsList) {
            auto group = configActions.group(name);
            auto passAction = PassAction::fromConfig(group);

            auto icon = QIcon::fromTheme(passAction.icon, QIcon::fromTheme("object-unlocked"));
            QAction *act = addAction(passAction.name, icon, passAction.name);
            act->setData(passAction.regex);
            this->orderedActions << act;
        }

    } else {
        this->orderedActions.clear();
    }

    if (cfg.readEntry(Config::showFileContentAction, false)) {
        QAction *act = addAction(Config::showFileContentAction, QIcon::fromTheme("document-new"),
                                 i18n("Show password file contents"));
        act->setData(Config::showFileContentAction);
        this->orderedActions << act;
    }

    setDefaultSyntax(Plasma::RunnerSyntax(QString(":q:"),
                                          i18n("Looks for a password matching :q:. Pressing ENTER copies the password to the clipboard.")));

    addSyntax(Plasma::RunnerSyntax(QString("pass :q:"),
                                          i18n("Looks for a password matching :q:. This way you avoid results from other runners")));
}

void Pass::init()
{
    reloadConfiguration();

    this->baseDir = QDir(QDir::homePath() + "/.password-store");
    auto _baseDir = getenv("PASSWORD_STORE_DIR");
    if (_baseDir != nullptr) {
        this->baseDir = QDir(_baseDir);
    }

    this->timeout = 45;
    auto _timeout = getenv("PASSWORD_STORE_CLIP_TIME");
    if (_timeout != nullptr) {
        QString str(_timeout);
        bool ok;
        auto _timeoutParsed = str.toInt(&ok);
        if (ok) {
            this->timeout = _timeoutParsed;
        }
    }

    this->passOtpIdentifier = "totp::";
    auto _passOtpIdentifier = getenv("PASSWORD_STORE_OTP_IDENTIFIER");
    if (_passOtpIdentifier != nullptr) {
        this->passOtpIdentifier = _passOtpIdentifier;
    }

    initPasswords();

    connect(&watcher, &QFileSystemWatcher::directoryChanged, this, &Pass::reinitPasswords);
}

void Pass::initPasswords()
{
    passwords.clear();

    watcher.addPath(this->baseDir.absolutePath());
    QDirIterator it(this->baseDir, QDirIterator::Subdirectories);
    while (it.hasNext()) {
        it.next();
        const auto fileInfo = it.fileInfo();
        if (fileInfo.isFile() && fileInfo.suffix() == QLatin1String("gpg")) {
            QString password = this->baseDir.relativeFilePath(fileInfo.absoluteFilePath());
            // Remove suffix ".gpg"
            password.chop(4);
            passwords.append(password);
        } else if (fileInfo.isDir() && it.fileName() != "." && it.fileName() != "..") {
            watcher.addPath(it.filePath());
        }
    }
}

void Pass::reinitPasswords(const QString &path)
{
    Q_UNUSED(path)

    lock.lockForWrite();
    initPasswords();
    lock.unlock();
}

void Pass::match(Plasma::RunnerContext &context)
{
    if (!context.isValid()) {
        return;
    }

    auto input = context.query();
    // If we use the prefix we want to remove it
    if (input.startsWith(queryPrefix)) {
        input = input.remove(queryPrefix).simplified();
    } else if (input.count() < 3 && !context.singleRunnerQueryMode()) {
        return;
    }

    QList<Plasma::QueryMatch> matches;

    lock.lockForRead();
    for (const auto &password: qAsConst(passwords)) {
        if (password.contains(input, Qt::CaseInsensitive)) {
            Plasma::QueryMatch match(this);
            match.setType(input.length() == password.length() ?
                          Plasma::QueryMatch::ExactMatch : Plasma::QueryMatch::CompletionMatch);
            match.setIcon(QIcon::fromTheme("object-locked"));
            match.setText(password);
            matches.append(match);
        }
    }
    lock.unlock();

    context.addMatches(matches);
}

void Pass::clip(const QString &msg)
{
    QClipboard *cb = QApplication::clipboard();
    cb->setText(msg);
    QTimer::singleShot(timeout * 1000, cb, [cb]() {
        cb->setText(QString());
    });
}

void Pass::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
    Q_UNUSED(context);
    const auto regexp = QRegularExpression("^" + QRegularExpression::escape(this->passOtpIdentifier) + ".*");
    const auto isOtp = !match.text().split('/').filter(regexp).isEmpty();

    auto *pass = new QProcess();
    QStringList args;
    if (isOtp) {
        args << "otp";
    }
    args << "show" << match.text();
    pass->start("pass", args);

    connect(pass, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
            [=](int exitCode, QProcess::ExitStatus exitStatus) {
                Q_UNUSED(exitStatus)

                if (exitCode == 0) {
                    const auto output = pass->readAllStandardOutput();
                    if (match.selectedAction() != nullptr) {
                        const auto data = match.selectedAction()->data().toString();
                        if (data == Config::showFileContentAction) {
                            QMessageBox::information(nullptr, match.text(), output);
                        } else {
                            QRegularExpression re(data, QRegularExpression::MultilineOption);
                            const auto matchre = re.match(output);

                            if (matchre.hasMatch()) {
                                clip(matchre.captured(1));
                                this->showNotification(match.text(), match.selectedAction()->text());
                            } else {
                                // Show some information to understand what went wrong.
                                qInfo() << "Regexp: " << data;
                                qInfo() << "Is regexp valid? " << re.isValid();
                                qInfo() << "The file: " << match.text();
                                // qInfo() << "Content: " << output;
                            }
                        }
                    } else {
                        const auto string = QString::fromUtf8(output.data());
                        const auto lines = string.split('\n', QString::SkipEmptyParts);
                        if (!lines.isEmpty()) {
                            clip(lines[0]);
                            this->showNotification(match.text());
                        }
                    }
                }

                pass->close();
                pass->deleteLater();
            });
}

QList<QAction *> Pass::actionsForMatch(const Plasma::QueryMatch &match)
{
    Q_UNUSED(match)

    return this->orderedActions;
}

void Pass::showNotification