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