QtPass 1.4.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#include "realpass.h"
2#include "qtpasssettings.h"
3#include "util.h"
4
5#include <QDir>
6#include <QFileInfo>
7#include <QRegularExpression>
8#include <utility>
9
10using namespace Enums;
11
12RealPass::RealPass() = default;
13
17void RealPass::GitInit() { executePass(GIT_INIT, {"git", "init"}); }
18
25}
26
30void RealPass::GitPull() { executePass(GIT_PULL, {"git", "pull"}); }
31
35void RealPass::GitPush() { executePass(GIT_PUSH, {"git", "push"}); }
36
46void RealPass::Show(QString file) {
47 executePass(PASS_SHOW, {"show", file}, "", true);
48}
49
54void RealPass::OtpGenerate(QString file) {
55 executePass(PASS_OTP_GENERATE, {"otp", file}, "", true);
56}
57
61void RealPass::Insert(QString file, QString newValue, bool overwrite) {
62 QStringList args = {"insert", "-m"};
63 if (overwrite)
64 args.append("-f");
65 args.append(file);
66 executePass(PASS_INSERT, args, newValue);
67}
68
72void RealPass::Remove(QString file, bool isDir) {
73 executePass(PASS_REMOVE, {"rm", (isDir ? "-rf" : "-f"), file});
74}
75
82void RealPass::Init(QString path, const QList<UserInfo> &users) {
83 // remove the passStore directory otherwise,
84 // pass would create a passStore/passStore/dir
85 // but you want passStore/dir
86 QString dirWithoutPassdir =
87 path.remove(0, QtPassSettings::getPassStore().size());
88 QStringList args = {"init", "--path=" + dirWithoutPassdir};
89 foreach (const UserInfo &user, users) {
90 if (user.enabled)
91 args.append(user.key_id);
92 }
93 executePass(PASS_INIT, args);
94}
95
102void RealPass::Move(const QString src, const QString dest, const bool force) {
103 QFileInfo srcFileInfo = QFileInfo(src);
104 QFileInfo destFileInfo = QFileInfo(dest);
105
106 // force mode?
107 // pass uses always the force mode, when call from eg. QT. so we have to check
108 // if this are to files
109 // and the user didnt want to move force
110 if (!force && srcFileInfo.isFile() && destFileInfo.isFile()) {
111 return;
112 }
113
114 QString passSrc = QDir(QtPassSettings::getPassStore())
115 .relativeFilePath(QDir(src).absolutePath());
116 QString passDest = QDir(QtPassSettings::getPassStore())
117 .relativeFilePath(QDir(dest).absolutePath());
118
119 // remove the .gpg because pass will not work
120 if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
121 passSrc.replace(Util::endsWithGpg(), "");
122 }
123 if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
124 passDest.replace(Util::endsWithGpg(), "");
125 }
126
127 QStringList args;
128 args << "mv";
129 if (force) {
130 args << "-f";
131 }
132 args << passSrc;
133 args << passDest;
134 executePass(PASS_MOVE, args);
135}
136
143void RealPass::Copy(const QString src, const QString dest, const bool force) {
144 QFileInfo srcFileInfo = QFileInfo(src);
145 QFileInfo destFileInfo = QFileInfo(dest);
146 // force mode?
147 // pass uses always the force mode, when call from eg. QT. so we have to check
148 // if this are to files
149 // and the user didnt want to move force
150 if (!force && srcFileInfo.isFile() && destFileInfo.isFile()) {
151 return;
152 }
153
154 QString passSrc = QDir(QtPassSettings::getPassStore())
155 .relativeFilePath(QDir(src).absolutePath());
156 QString passDest = QDir(QtPassSettings::getPassStore())
157 .relativeFilePath(QDir(dest).absolutePath());
158
159 // remove the .gpg because pass will not work
160 if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
161 passSrc.replace(Util::endsWithGpg(), "");
162 }
163 if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
164 passDest.replace(Util::endsWithGpg(), "");
165 }
166 QStringList args;
167 args << "cp";
168 if (force) {
169 args << "-f";
170 }
171 args << passSrc;
172 args << passDest;
173 executePass(PASS_COPY, args);
174}
175
180void RealPass::executePass(PROCESS id, const QStringList &args, QString input,
181 bool readStdout, bool readStderr) {
183 std::move(input), readStdout, readStderr);
184}
static int executeBlocking(QString app, const QStringList &args, QString input=QString(), QString *process_out=Q_NULLPTR, QString *process_err=Q_NULLPTR)
Executor::executeBlocking blocking version of the executor, takes input and presents it as stdin.
Definition: executor.cpp:171
void executeWrapper(PROCESS id, const QString &app, const QStringList &args, bool readStdout=true, bool readStderr=true)
Definition: pass.cpp:31
Executor exec
Definition: pass.h:25
static QString getPassStore(const QString &defaultValue=QVariant().toString())
static QString getPassExecutable(const QString &defaultValue=QVariant().toString())
virtual void Insert(QString file, QString newValue, bool overwrite=false) Q_DECL_OVERRIDE
RealPass::Insert pass insert.
Definition: realpass.cpp:61
virtual void OtpGenerate(QString file) Q_DECL_OVERRIDE
RealPass::OtpGenerate pass otp.
Definition: realpass.cpp:54
virtual void GitPull() Q_DECL_OVERRIDE
RealPass::GitPull pass git pull wrapper.
Definition: realpass.cpp:30
virtual void GitPush() Q_DECL_OVERRIDE
RealPass::GitPush pass git push wrapper.
Definition: realpass.cpp:35
virtual void Init(QString path, const QList< UserInfo > &users) Q_DECL_OVERRIDE
RealPass::Init initialize pass repository.
Definition: realpass.cpp:82
virtual void GitInit() Q_DECL_OVERRIDE
RealPass::GitInit pass git init wrapper.
Definition: realpass.cpp:17
virtual void Remove(QString file, bool isDir=false) Q_DECL_OVERRIDE
RealPass::Remove pass remove wrapper.
Definition: realpass.cpp:72
void Copy(const QString src, const QString dest, const bool force=false) Q_DECL_OVERRIDE
RealPass::Copy copy a file (or folder)
Definition: realpass.cpp:143
virtual void GitPull_b() Q_DECL_OVERRIDE
RealPass::GitInit pass git pull wrapper which blocks until process finishes.
Definition: realpass.cpp:23
void Move(const QString src, const QString dest, const bool force=false) Q_DECL_OVERRIDE
RealPass::Move move a file (or folder)
Definition: realpass.cpp:102
virtual void Show(QString file) Q_DECL_OVERRIDE
RealPass::Show pass show.
Definition: realpass.cpp:46
static const QRegularExpression & endsWithGpg()
Definition: util.cpp:198
Enumerators for configuration and runtime items.
PROCESS
Definition: enums.h:16
@ PASS_INIT
Definition: enums.h:26
@ PASS_OTP_GENERATE
Definition: enums.h:34
@ PASS_INSERT
Definition: enums.h:24
@ GIT_INIT
Definition: enums.h:17
@ PASS_COPY
Definition: enums.h:29
@ PASS_MOVE
Definition: enums.h:28
@ PASS_REMOVE
Definition: enums.h:25
@ PASS_SHOW
Definition: enums.h:23
@ GIT_PULL
Definition: enums.h:21
@ GIT_PUSH
Definition: enums.h:22
Stores key info lines including validity, creation date and more.
Definition: userinfo.h:11
bool enabled
UserInfo::enabled.
Definition: userinfo.h:50
QString key_id
UserInfo::key_id hexadecimal representation.
Definition: userinfo.h:36