Line data Source code
1 : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : /**
4 : * @file
5 : * @brief Initialising a new profile's store (.gpg-id, signature, Git) with
6 : * blocking processes, apart from the active store's backend.
7 : */
8 : #ifndef SRC_PROFILEINIT_H_
9 : #define SRC_PROFILEINIT_H_
10 :
11 : #include <QCoreApplication>
12 : #include <QList>
13 : #include <QProcessEnvironment>
14 : #include <QString>
15 :
16 : class QProcess;
17 : struct AppSettings;
18 : struct UserInfo;
19 :
20 : /**
21 : * @class ProfileInit
22 : * @ingroup settings
23 : * @brief Turn a directory into a password store without going through the
24 : * Pass backend of the currently active store.
25 : *
26 : * The backend is a shared singleton bound to the active store (settings
27 : * snapshot, PASSWORD_STORE_DIR, one FIFO of asynchronous processes), so a
28 : * new profile cannot be initialised through it without racing the active
29 : * store (#1774). Everything here is synchronous and touches only @p dir.
30 : */
31 : class ProfileInit {
32 32 : Q_DECLARE_TR_FUNCTIONS(ProfileInit)
33 :
34 : public:
35 : /**
36 : * @brief The environment QtPass runs gpg in outside the active store's
37 : * backend: the system's, with GNUPGHOME set to the configured GPG home
38 : * when there is one, so lookups and signatures use the keyring that signs.
39 : * @param s The settings naming the GPG home.
40 : * @return The environment for a gpg process.
41 : */
42 : static auto gpgEnvironment(const AppSettings &s) -> QProcessEnvironment;
43 : /**
44 : * @brief Check if a profile path needs initialization.
45 : * @param path The profile path to check.
46 : * @return true if the path exists but has no .gpg-id file.
47 : */
48 : static auto needsInit(const QString &path) -> bool;
49 :
50 : /**
51 : * @brief Write the recipients and, if wanted, put the folder under git.
52 : *
53 : * Writes `<dir>/.gpg-id` with the enabled key ids, signs it when
54 : * @p s.passSigningKey is set, and when @p useGit runs `git init`,
55 : * `git add` and `git commit` inside @p dir with blocking processes.
56 : * Existing `*.gpg` files are left alone: re-encrypting them needs the
57 : * backend, i.e. the profile has to be the active store; @p note says so.
58 : * @param dir Directory to initialise; created if missing.
59 : * @param users Key list; only entries with enabled == true are written.
60 : * @param settings Settings for the gpg/git executables and the signing
61 : * key; with no signing key set, one exported for pass counts
62 : * (GpgIdSigner::settingOrExported()).
63 : * @param useGit Whether to create a repository and commit the file.
64 : * @param note Receives a translated message for the user (empty on a quiet
65 : * success): the error, or the warning about existing entries.
66 : * @return true when the .gpg-id (and signature/commit if requested) is in
67 : * place.
68 : */
69 : static auto initialise(const QString &dir, const QList<UserInfo> &users,
70 : const AppSettings &settings, bool useGit,
71 : QString *note) -> bool;
72 :
73 : /**
74 : * @brief Put an existing store under Git: `git init`, stage the `.gpg`
75 : * entries and the `.gpg-id` files (nothing else that may lie around in the
76 : * folder) and make a first commit, with blocking processes of their own in
77 : * @p dir.
78 : * @param dir The store.
79 : * @param s Settings for the git executable.
80 : * @param note Receives a translated error on failure.
81 : * @return true when the repository exists and the commit is made.
82 : */
83 : static auto initGit(const QString &dir, const AppSettings &s, QString *note)
84 : -> bool;
85 :
86 : /**
87 : * @brief Whether git has a name and e-mail to commit with, i.e.
88 : * `user.name` and `user.email` resolve (globally or in @p dir).
89 : * @param dir Directory to ask in; a repository's own config counts.
90 : * @param s Settings for the git executable.
91 : * @return true when a commit would not fail for lack of an identity.
92 : */
93 : static auto gitIdentityConfigured(const QString &dir, const AppSettings &s)
94 : -> bool;
95 :
96 : private:
97 : ProfileInit() = default;
98 : /**
99 : * @brief Whether the profile has a signing key whose secret key gpg does
100 : * not hold, so no list may be written: writing one takes the next
101 : * generation, and an unsigned list then leaves this device's record ahead
102 : * of the store's own.
103 : * @param s The settings naming gpg, its home and the signing key.
104 : * @param note Receives the translated reason, naming a configured GPG
105 : * home that does not exist.
106 : * @return true when initialising must stop.
107 : */
108 : static auto signingKeyUnavailable(const AppSettings &s, QString *note)
109 : -> bool;
110 : /**
111 : * @brief The encrypted entries under @p dir that the new root list
112 : * covers: visible regular `.gpg` files, sub-folders included, except
113 : * those under a sub-folder whose own `.gpg-id` decides for them (as pass
114 : * reads it with @p viaPass, as the native backend does without).
115 : * @param dir The store, absolute.
116 : * @param viaPass Whether the profile uses the `pass` backend.
117 : * @return The entries that kept their old recipients.
118 : */
119 : static auto entriesOnThisList(const QString &dir, bool viaPass)
120 : -> QStringList;
121 : /**
122 : * @brief Write the enabled recipients to @p gpgIdFile, whole or not at
123 : * all; with @p signed_ the list carries the generation and folder header a
124 : * signed store's lists are checked against (GpgIdGeneration).
125 : * @param gpgIdFile The `.gpg-id` to write.
126 : * @param users The recipients; the enabled ones are written.
127 : * @param signed_ Whether the store gets a signing key, i.e. the header.
128 : * @param note Receives the reason on failure.
129 : * @param written Receives the exact bytes written, if not null.
130 : * @return Whether the file was written.
131 : */
132 : static auto writeGpgId(const QString &gpgIdFile, const QList<UserInfo> &users,
133 : bool signed_, QString *note,
134 : QByteArray *written = nullptr) -> bool;
135 : /**
136 : * @brief Sign the list just written as @p gpgIdFile, over the bytes
137 : * @p contents that were written.
138 : * @param gpgIdFile The `.gpg-id`; `<gpgIdFile>.sig` is written.
139 : * @param contents The bytes written as that file.
140 : * @param s The settings naming gpg and the signing key.
141 : * @param note Receives the reason on failure.
142 : * @return Whether the signature is in place.
143 : */
144 : static auto signGpgId(const QString &gpgIdFile, const QByteArray &contents,
145 : const AppSettings &s, QString *note) -> bool;
146 : /**
147 : * @brief `git init` in @p dir, stage the `.gpg-id` and its signature, and
148 : * commit them (signed when `pass.signcommits` is set), with a QProcess of
149 : * its own in the store's Git environment.
150 : * @param dir The store, absolute.
151 : * @param gpgIdFile The `.gpg-id` just written.
152 : * @param sigFile Its `.sig`, or empty when the store is not signed.
153 : * @param s Settings for the git executable and the GPG home.
154 : * @param note Receives a translated error naming the failed git command.
155 : * @return Whether the commit was made.
156 : */
157 : static auto commitGpgId(const QString &dir, const QString &gpgIdFile,
158 : const QString &sigFile, const AppSettings &s,
159 : QString *note) -> bool;
160 : /**
161 : * @brief Run one set-up git command on @p git and wait for it: `init` and
162 : * `add`, which read what lies in the folder, for at most two minutes, as
163 : * the window waits too; a commit, which git may sign and so wait for the
164 : * passphrase, without a limit.
165 : * @param git The process, set up for the store.
166 : * @param s Settings for the git executable.
167 : * @param dir The store, named in the note.
168 : * @param args The git arguments; the first names the command.
169 : * @param note Receives a translated error naming the command, or that it
170 : * ran out of time.
171 : * @return Whether the command succeeded.
172 : */
173 : static auto runGit(QProcess &git, const AppSettings &s, const QString &dir,
174 : const QStringList &args, QString *note) -> bool;
175 : /**
176 : * @brief Whether staging @p files in @p dir is refused because git could
177 : * wait on something there for good: a .gitattributes or .gitignore on
178 : * the way that is not a regular file, or a repository in a sub-folder.
179 : * @param dir The store.
180 : * @param files Paths relative to @p dir that git is to stage.
181 : * @param note Receives a translated reason naming the path.
182 : * @return true when git must not be run.
183 : */
184 : static auto refusedForGit(const QString &dir, const QStringList &files,
185 : QString *note) -> bool;
186 : };
187 :
188 : #endif // SRC_PROFILEINIT_H_
|