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;
23
24RealPass::RealPass() = default;
25
29void RealPass::GitInit() { executePass(GIT_INIT, {"git", "init"}); }
30
37 {"git", "pull"});
38 if (result != 0) {
39#ifdef QT_DEBUG
40 dbg() << "Git pull failed with code:" << result;
41#endif
42 }
43}
44
48void RealPass::GitPull() { executePass(GIT_PULL, {"git", "pull"}); }
49
53void RealPass::GitPush() { executePass(GIT_PUSH, {"git", "push"}); }
54
64void RealPass::Show(QString file) {
65 executePass(PASS_SHOW, {"show", file}, "", true);
66}
67
72void RealPass::OtpGenerate(QString file) {
73 executePass(PASS_OTP_GENERATE, {"otp", file}, "", true);
74}
75
79void RealPass::Insert(QString file, QString newValue, bool overwrite) {
80 QStringList args = {"insert", "-m"};
81 if (overwrite) {
82 args.append("-f");
83 }
84 args.append(file);
85 executePass(PASS_INSERT, args, newValue);
86}
87
91void RealPass::Remove(QString file, bool isDir) {
92 executePass(PASS_REMOVE, {"rm", (isDir ? "-rf" : "-f"), file});
93}
94
101void RealPass::Init(QString path, const QList<UserInfo> &users) {
102 // remove the passStore directory otherwise,
103 // pass would create a passStore/passStore/dir
104 // but you want passStore/dir
105 QString dirWithoutPassdir =
106 path.remove(0, QtPassSettings::getPassStore().size());
107 QStringList args = {"init", "--path=" + dirWithoutPassdir};
108 foreach (const UserInfo &user, users) {
109 if (user.enabled) {
110 args.append(user.key_id);
111 }
112 }
113 executePass(PASS_INIT, args);
114}
115
122void RealPass::Move(const QString src, const QString dest, const bool force) {
123 QFileInfo srcFileInfo = QFileInfo(src);
124 QFileInfo destFileInfo = QFileInfo(dest);
125
126 // force mode?
127 // pass uses always the force mode, when call from eg. QT. so we have to
128 // check if this are to files and the user didnt want to move force
129 if (!force && srcFileInfo.isFile() && destFileInfo.isFile()) {
130 return;
131 }
132
133 QString passSrc = QDir(QtPassSettings::getPassStore())
134 .relativeFilePath(QDir(src).absolutePath());
135 QString passDest = QDir(QtPassSettings::getPassStore())
136 .relativeFilePath(QDir(dest).absolutePath());
137
138 // remove the .gpg because pass will not work
139 if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
140 passSrc.replace(Util::endsWithGpg(), "");
141 }
142 if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
143 passDest.replace(Util::endsWithGpg(), "");
144 }
145
146 QStringList args;
147 args << "mv";
148 if (force) {
149 args << "-f";
150 }
151 args << passSrc;
152 args << passDest;
153 executePass(PASS_MOVE, args);
154}
155
162void RealPass::Copy(const QString src, const QString dest, const bool force) {
163 QFileInfo srcFileInfo = QFileInfo(src);
164 QFileInfo destFileInfo = QFileInfo(dest);
165 // force mode?
166 // pass uses always the force mode, when call from eg. QT. so we have to
167 // check if this are to files and the user didnt want to move force
168 if (!force && srcFileInfo.isFile() && destFileInfo.isFile()) {
169 return;
170 }
171
172 QString passSrc = QDir(QtPassSettings::getPassStore())
173 .relativeFilePath(QDir(src).absolutePath());
174 QString passDest = QDir(QtPassSettings::getPassStore())
175 .relativeFilePath(QDir(dest).absolutePath());
176
177 // remove the .gpg because pass will not work
178 if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
179 passSrc.replace(Util::endsWithGpg(), "");
180 }
181 if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
182 passDest.replace(Util::endsWithGpg(), "");
183 }
184 QStringList args;
185 args << "cp";
186 if (force) {
187 args << "-f";
188 }
189 args << passSrc;
190 args << passDest;
191 executePass(PASS_COPY, args);
192}
193
202void RealPass::executePass(PROCESS id, const QStringList &args, QString input,
203 bool readStdout, bool readStderr) {
205 std::move(input), readStdout, readStderr);
206}
static auto executeBlocking(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:57
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:101
void OtpGenerate(QString file) override
Generate OTP code.
Definition realpass.cpp:72
void Insert(QString file, QString newValue, bool overwrite=false) override
Insert new password.
Definition realpass.cpp:79
void Show(QString file) override
Show decrypted password.
Definition realpass.cpp:64
void Copy(const QString src, const QString dest, const bool force=false) override
Copy password file or directory.
Definition realpass.cpp:162
void GitPull_b() override
Pull with rebase.
Definition realpass.cpp:35
RealPass()
Construct a RealPass instance.
void Remove(QString file, bool isDir=false) override
Remove password or directory.
Definition realpass.cpp:91
void GitInit() override
Initialize Git repository in password store.
Definition realpass.cpp:29
void GitPull() override
Pull changes from remote.
Definition realpass.cpp:48
void Move(const QString src, const QString dest, const bool force=false) override
Move password file or directory.
Definition realpass.cpp:122
void GitPush() override
Push changes to remote.
Definition realpass.cpp:53
static auto endsWithGpg() -> const QRegularExpression &
Returns a regex to match .gpg file extensions.
Definition util.cpp:249
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:44
@ 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_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:44
@ 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_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