Line data Source code
1 : // SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : #include "realpass.h"
4 : #include "qtpasssettings.h"
5 : #include "util.h"
6 :
7 : #include <QDir>
8 : #include <QFileInfo>
9 : #include <QRegularExpression>
10 : #include <utility>
11 :
12 : using Enums::GIT_INIT;
13 : using Enums::GIT_PULL;
14 : using Enums::GIT_PUSH;
15 : using Enums::PASS_COPY;
16 : using Enums::PASS_INIT;
17 : using Enums::PASS_INSERT;
18 : using Enums::PASS_MOVE;
19 : using Enums::PASS_OTP_GENERATE;
20 : using Enums::PASS_REMOVE;
21 : using Enums::PASS_SHOW;
22 :
23 55 : RealPass::RealPass() = default;
24 :
25 : /**
26 : * @brief RealPass::GitInit pass git init wrapper
27 : */
28 0 : void RealPass::GitInit() { executePass(GIT_INIT, {"git", "init"}); }
29 :
30 : /**
31 : * @brief RealPass::GitInit pass git pull wrapper which blocks until process
32 : * finishes
33 : */
34 0 : void RealPass::GitPull_b() {
35 0 : Executor::executeBlocking(QtPassSettings::getPassExecutable(),
36 : {"git", "pull"});
37 0 : }
38 :
39 : /**
40 : * @brief RealPass::GitPull pass git pull wrapper
41 : */
42 0 : void RealPass::GitPull() { executePass(GIT_PULL, {"git", "pull"}); }
43 :
44 : /**
45 : * @brief RealPass::GitPush pass git push wrapper
46 : */
47 0 : void RealPass::GitPush() { executePass(GIT_PUSH, {"git", "push"}); }
48 :
49 : /**
50 : * @brief RealPass::Show pass show
51 : *
52 : * @param file file to decrypt
53 : *
54 : * @return if block is set, returns exit status of internal decryption
55 : * process
56 : * otherwise returns QProcess::NormalExit
57 : */
58 0 : void RealPass::Show(QString file) {
59 0 : executePass(PASS_SHOW, {"show", file}, "", true);
60 0 : }
61 :
62 : /**
63 : * @brief RealPass::OtpGenerate pass otp
64 : * @param file file containig OTP uri
65 : */
66 0 : void RealPass::OtpGenerate(QString file) {
67 0 : executePass(PASS_OTP_GENERATE, {"otp", file}, "", true);
68 0 : }
69 :
70 : /**
71 : * @brief RealPass::Insert pass insert
72 : */
73 0 : void RealPass::Insert(QString file, QString newValue, bool overwrite) {
74 0 : QStringList args = {"insert", "-m"};
75 0 : if (overwrite) {
76 0 : args.append("-f");
77 : }
78 : args.append(file);
79 0 : executePass(PASS_INSERT, args, newValue);
80 0 : }
81 :
82 : /**
83 : * @brief RealPass::Remove pass remove wrapper
84 : */
85 0 : void RealPass::Remove(QString file, bool isDir) {
86 0 : executePass(PASS_REMOVE, {"rm", (isDir ? "-rf" : "-f"), file});
87 0 : }
88 :
89 : /**
90 : * @brief RealPass::Init initialize pass repository
91 : *
92 : * @param path Absolute path to new password-store
93 : * @param users list of users with ability to decrypt new password-store
94 : */
95 0 : void RealPass::Init(QString path, const QList<UserInfo> &users) {
96 : // remove the passStore directory otherwise,
97 : // pass would create a passStore/passStore/dir
98 : // but you want passStore/dir
99 : QString dirWithoutPassdir =
100 0 : path.remove(0, QtPassSettings::getPassStore().size());
101 0 : QStringList args = {"init", "--path=" + dirWithoutPassdir};
102 0 : foreach (const UserInfo &user, users) {
103 0 : if (user.enabled) {
104 0 : args.append(user.key_id);
105 : }
106 : }
107 0 : executePass(PASS_INIT, args);
108 0 : }
109 :
110 : /**
111 : * @brief RealPass::Move move a file (or folder)
112 : * @param src source file or folder
113 : * @param dest destination file or folder
114 : * @param force overwrite
115 : */
116 0 : void RealPass::Move(const QString src, const QString dest, const bool force) {
117 0 : QFileInfo srcFileInfo = QFileInfo(src);
118 0 : QFileInfo destFileInfo = QFileInfo(dest);
119 :
120 : // force mode?
121 : // pass uses always the force mode, when call from eg. QT. so we have to
122 : // check if this are to files and the user didnt want to move force
123 0 : if (!force && srcFileInfo.isFile() && destFileInfo.isFile()) {
124 : return;
125 : }
126 :
127 0 : QString passSrc = QDir(QtPassSettings::getPassStore())
128 0 : .relativeFilePath(QDir(src).absolutePath());
129 0 : QString passDest = QDir(QtPassSettings::getPassStore())
130 0 : .relativeFilePath(QDir(dest).absolutePath());
131 :
132 : // remove the .gpg because pass will not work
133 0 : if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
134 0 : passSrc.replace(Util::endsWithGpg(), "");
135 : }
136 0 : if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
137 0 : passDest.replace(Util::endsWithGpg(), "");
138 : }
139 :
140 0 : QStringList args;
141 0 : args << "mv";
142 0 : if (force) {
143 0 : args << "-f";
144 : }
145 : args << passSrc;
146 : args << passDest;
147 0 : executePass(PASS_MOVE, args);
148 0 : }
149 :
150 : /**
151 : * @brief RealPass::Copy copy a file (or folder)
152 : * @param src source file or folder
153 : * @param dest destination file or folder
154 : * @param force overwrite
155 : */
156 0 : void RealPass::Copy(const QString src, const QString dest, const bool force) {
157 0 : QFileInfo srcFileInfo = QFileInfo(src);
158 0 : QFileInfo destFileInfo = QFileInfo(dest);
159 : // force mode?
160 : // pass uses always the force mode, when call from eg. QT. so we have to
161 : // check if this are to files and the user didnt want to move force
162 0 : if (!force && srcFileInfo.isFile() && destFileInfo.isFile()) {
163 : return;
164 : }
165 :
166 0 : QString passSrc = QDir(QtPassSettings::getPassStore())
167 0 : .relativeFilePath(QDir(src).absolutePath());
168 0 : QString passDest = QDir(QtPassSettings::getPassStore())
169 0 : .relativeFilePath(QDir(dest).absolutePath());
170 :
171 : // remove the .gpg because pass will not work
172 0 : if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
173 0 : passSrc.replace(Util::endsWithGpg(), "");
174 : }
175 0 : if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
176 0 : passDest.replace(Util::endsWithGpg(), "");
177 : }
178 0 : QStringList args;
179 0 : args << "cp";
180 0 : if (force) {
181 0 : args << "-f";
182 : }
183 : args << passSrc;
184 : args << passDest;
185 0 : executePass(PASS_COPY, args);
186 0 : }
187 :
188 : /**
189 : * @brief RealPass::executePass easy wrapper for running pass
190 : * @param args
191 : */
192 0 : void RealPass::executePass(PROCESS id, const QStringList &args, QString input,
193 : bool readStdout, bool readStderr) {
194 0 : executeWrapper(id, QtPassSettings::getPassExecutable(), args,
195 : std::move(input), readStdout, readStderr);
196 0 : }
|