QtPass 1.6.0
Multi-platform GUI for pass, the standard unix password manager.
Loading...
Searching...
No Matches
realpass.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
3#include "realpass.h"
4#include "debughelper.h"
5#include "qtpasssettings.h"
6#include "util.h"
7
8#include <QDir>
9#include <QFileInfo>
10#include <QRegularExpression>
11#include <utility>
12
13using Enums::GIT_INIT;
14using Enums::GIT_PULL;
15using Enums::GIT_PUSH;
24
25RealPass::RealPass() = default;
26
30void RealPass::GitInit() { executePass(GIT_INIT, {"git", "init"}); }
31
38 {"git", "pull"});
39 if (result != 0) {
40#ifdef QT_DEBUG
41 dbg() << "Git pull failed with code:" << result;
42#endif
43 }
44}
45
49void RealPass::GitPull() { executePass(GIT_PULL, {"git", "pull"}); }
50
54void RealPass::GitPush() { executePass(GIT_PUSH, {"git", "push"}); }
55
65void RealPass::Show(QString file) {
66 executePass(PASS_SHOW, {"show", file}, "", true);
67}
68
73void RealPass::OtpGenerate(QString file) {
74 executePass(PASS_OTP_GENERATE, {"otp", file}, "", true);
75}
76
80void RealPass::Insert(QString file, QString newValue, bool overwrite) {
81 QStringList args = {"insert", "-m"};
82 if (overwrite) {
83 args.append("-f");
84 }
85 args.append(file);
86 executePass(PASS_INSERT, args, newValue);
87}
88
92void RealPass::Remove(QString file, bool isDir) {
93 executePass(PASS_REMOVE, {"rm", (isDir ? "-rf" : "-f"), file});
94}
95
102void RealPass::Init(QString path, const QList<UserInfo> &users) {
103 // remove the passStore directory otherwise,
104 // pass would create a passStore/passStore/dir
105 // but you want passStore/dir
106 QString dirWithoutPassdir =
107 path.remove(0, QtPassSettings::getPassStore().size());
108 QStringList args = {"init", "--path=" + dirWithoutPassdir};
109 foreach (const UserInfo &user, users) {
110 if (user.enabled) {
111 args.append(user.key_id);
112 }
113 }
114 executePass(PASS_INIT, args);
115}
116
123void RealPass::Move(const QString src, const QString dest, const bool force) {
124 QFileInfo srcFileInfo = QFileInfo(src);
125 QFileInfo destFileInfo = QFileInfo(dest);
126
127 // force mode?
128 // pass uses always the force mode, when call from eg. QT. so we have to
129 // check if this are to files and the user didnt want to move force
130 if (!force && srcFileInfo.isFile() && destFileInfo.isFile()) {
131 return;
132 }
133
134 QString passSrc = QDir(QtPassSettings::getPassStore())
135 .relativeFilePath(QDir(src).absolutePath());
136 QString passDest = QDir(QtPassSettings::getPassStore())
137 .relativeFilePath(QDir(dest).absolutePath());
138
139 // remove the .gpg because pass will not work
140 if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
141 passSrc.replace(Util::endsWithGpg(), "");
142 }
143 if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
144 passDest.replace(Util::endsWithGpg(), "");
145 }
146
147 QStringList args;
148 args << "mv";
149 if (force) {
150 args << "-f";
151 }
152 args << passSrc;
153 args << passDest;
154 executePass(PASS_MOVE, args);
155}
156
163void RealPass::Copy(const QString src, const QString dest, const bool force) {
164 QFileInfo srcFileInfo = QFileInfo(src);
165 QFileInfo destFileInfo = QFileInfo(dest);
166 // force mode?
167 // pass uses always the force mode, when call from eg. QT. so we have to
168 // check if this are to files and the user didnt want to move force
169 if (!force && srcFileInfo.isFile() && destFileInfo.isFile()) {
170 return;
171 }
172
173 QString passSrc = QDir(QtPassSettings::getPassStore())
174 .relativeFilePath(QDir(src).absolutePath());
175 QString passDest = QDir(QtPassSettings::getPassStore())
176 .relativeFilePath(QDir(dest).absolutePath());
177
178 // remove the .gpg because pass will not work
179 if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
180 passSrc.replace(Util::endsWithGpg(), "");
181 }
182 if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
183 passDest.replace(Util::endsWithGpg(), "");
184 }
185 QStringList args;
186 args << "cp";
187 if (force) {
188 args << "-f";
189 }
190 args << passSrc;
191 args << passDest;
192 executePass(PASS_COPY, args);
193}
194
200void RealPass::Grep(QString pattern, bool caseInsensitive) {
201 QStringList args = {"grep"};
202 if (caseInsensitive)
203 args << "-i";
204 args << "--" << pattern;
205 executePass(PASS_GREP, args, QString(), true);
206}
207
216void RealPass::executePass(PROCESS id, const QStringList &args, QString input,
217 bool readStdout, bool readStderr) {
219 std::move(input), readStdout, readStderr);
220}
static auto executeBlocking(const QString &app, const QStringList &args, const QString &input=QString(), QString *process_out=nullptr, QString *process_err=nullptr) -> int
Executor::executeBlocking blocking version of the executor, takes input and presents it as stdin.
Definition executor.cpp:223
void executeWrapper(PROCESS id, const QString &app, const QStringList &args, bool readStdout=true, bool readStderr=true)
Execute external wrapper command.
Definition pass.cpp:96
static auto getPassStore(const QString &defaultValue=QVariant().toString()) -> QString
Get password store directory path.
static auto getPassExecutable(const QString &defaultValue=QVariant().toString()) -> QString
Get pass executable path.
void Init(QString path, const QList< UserInfo > &users) override
Initialize password store.
Definition realpass.cpp:102
void OtpGenerate(QString file) override
Generate OTP code.
Definition realpass.cpp:73
void Insert(QString file, QString newValue, bool overwrite=false) override
Insert new password.
Definition realpass.cpp:80
void Show(QString file) override
Show decrypted password.
Definition realpass.cpp:65
void Copy(const QString src, const QString dest, const bool force=false) override
Copy password file or directory.
Definition realpass.cpp:163
void GitPull_b() override
Pull with rebase.
Definition realpass.cpp:36
RealPass()
Construct a RealPass instance.
void Remove(QString file, bool isDir=false) override
Remove password or directory.
Definition realpass.cpp:92
void GitInit() override
Initialize Git repository in password store.
Definition realpass.cpp:30
void GitPull() override
Pull changes from remote.
Definition realpass.cpp:49
void Move(const QString src, const QString dest, const bool force=false) override
Move password file or directory.
Definition realpass.cpp:123
void Grep(QString pattern, bool caseInsensitive=false) override
Search password content via 'pass grep'.
Definition realpass.cpp:200
void GitPush() override
Push changes to remote.
Definition realpass.cpp:54
static auto endsWithGpg() -> const QRegularExpression &
Returns a regex to match .gpg file extensions.
Definition util.cpp:252
Debug utilities for QtPass.
#define dbg()
Simple debug macro that includes file and line number.
Definition debughelper.h:21
@ PASS_INIT
Definition enums.h:36
@ PASS_OTP_GENERATE
Definition enums.h:42
@ PASS_INSERT
Definition enums.h:34
@ GIT_INIT
Definition enums.h:27
@ PASS_COPY
Definition enums.h:39
@ PASS_MOVE
Definition enums.h:38
@ PASS_GREP
Definition enums.h:43
@ PASS_REMOVE
Definition enums.h:35
@ PASS_SHOW
Definition enums.h:33
@ GIT_PULL
Definition enums.h:31
@ GIT_PUSH
Definition enums.h:32
@ PASS_INIT
Definition enums.h:36
@ PASS_OTP_GENERATE
Definition enums.h:42
@ PASS_INSERT
Definition enums.h:34
@ GIT_INIT
Definition enums.h:27
@ PASS_COPY
Definition enums.h:39
@ PASS_MOVE
Definition enums.h:38
@ PASS_GREP
Definition enums.h:43
@ PASS_REMOVE
Definition enums.h:35
@ PASS_SHOW
Definition enums.h:33
@ GIT_PULL
Definition enums.h:31
@ GIT_PUSH
Definition enums.h:32
PROCESS
Identifies different subprocess operations used in QtPass.
Definition enums.h:26
Stores key info lines including validity, creation date and more.
Definition userinfo.h:13
bool enabled
UserInfo::enabled Whether this user/key is enabled for normal use. True when the key should be treate...
Definition userinfo.h:58
QString key_id
UserInfo::key_id hexadecimal representation of the GnuPG key identifier.
Definition userinfo.h:41