4#include <QRandomGenerator>
5#include <QRegularExpression>
17Pass::Pass() : wrapperRunning(false), env(QProcess::systemEnvironment()) {
19 static_cast<void (
Executor::*)(
int,
int,
const QString &,
20 const QString &)
>(&Executor::finished),
28 env.append(
"WSLENV=PASSWORD_STORE_DIR/p");
32 const QStringList &args,
bool readStdout,
38 const QStringList &args, QString input,
39 bool readStdout,
bool readStderr) {
50 if (QFile(
"/usr/local/MacGPG2/bin").exists())
51 env.replaceInStrings(
"PATH=",
"PATH=/usr/local/MacGPG2/bin:");
53 if (env.filter(
"/usr/local/bin").isEmpty())
54 env.replaceInStrings(
"PATH=",
"PATH=/usr/local/bin:");
59 absHome.makeAbsolute();
60 env <<
"GNUPGHOME=" + absHome.path();
78 args.append(
"--secure");
84 args.append(
"--symbols");
85 args.append(QString::number(length));
89 static const QRegularExpression literalNewLines{
"[\\n\\r]"};
90 passwd.remove(literalNewLines);
94 qDebug() << __FILE__ <<
":" << __LINE__ <<
"\t"
100 if (charset.length() > 0) {
104 tr(
"No characters chosen"),
105 tr(
"Can't generate password, there are no characters to choose from "
106 "set in the configuration!"));
118 {
"--gen-key",
"--no-tty",
"--batch"}, batch);
131 QList<UserInfo> users;
132 QStringList args = {
"--no-tty",
"--with-colons",
"--with-fingerprint"};
133 args.append(secret ?
"--list-secret-keys" :
"--list-keys");
135 for (
const QString &keystring : qAsConst(keystrings)) {
136 if (!keystring.isEmpty()) {
137 args.append(keystring);
144 static const QRegularExpression newLines{
"[\r\n]"};
145#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
146 const QStringList keys = p_out.split(newLines, Qt::SkipEmptyParts);
148 const QStringList keys = p_out.split(newLines, QString::SkipEmptyParts);
151 for (
const QString &key : keys) {
152 QStringList props = key.split(
':');
153 if (props.size() < 10)
155 if (props[0] == (secret ?
"sec" :
"pub")) {
156 if (!current_user.
key_id.isEmpty())
157 users.append(current_user);
159 current_user.
key_id = props[4];
160 current_user.
name = props[9].toUtf8();
161 current_user.
validity = props[1][0].toLatin1();
162 current_user.
created.setSecsSinceEpoch(props[5].toUInt());
163 current_user.
expiry.setSecsSinceEpoch(props[6].toUInt());
164 }
else if (current_user.
name.isEmpty() && props[0] ==
"uid") {
165 current_user.
name = props[9];
166 }
else if ((props[0] ==
"fpr") && props[9].endsWith(current_user.
key_id)) {
167 current_user.
key_id = props[9];
170 if (!current_user.
key_id.isEmpty())
171 users.append(current_user);
182 return listKeys(QStringList(keystring), secret);
196 const QString &err) {
197 auto pid =
static_cast<PROCESS>(id);
238 dbg() <<
"Unhandled process type" << pid;
250 QStringList envSigningKey = env.filter(
"PASSWORD_STORE_SIGNING_KEY=");
252 if (envSigningKey.isEmpty()) {
253 if (!currentSigningKey.isEmpty()) {
256 env.append(
"PASSWORD_STORE_SIGNING_KEY=" + currentSigningKey);
259 if (currentSigningKey.isEmpty()) {
262 env.removeAll(envSigningKey.first());
266 env.replaceInStrings(envSigningKey.first(),
267 "PASSWORD_STORE_SIGNING_KEY=" + currentSigningKey);
271 QStringList store = env.filter(
"PASSWORD_STORE_DIR=");
272 if (store.isEmpty()) {
279 env.replaceInStrings(store.first(),
"PASSWORD_STORE_DIR=" +
294 QFileInfo(QDir::fromNativeSeparators(for_file).startsWith(passStore)
299 while (gpgIdDir.exists() && gpgIdDir.absolutePath().startsWith(passStore)) {
300 if (QFile(gpgIdDir.absoluteFilePath(
".gpg-id")).exists()) {
304 if (!gpgIdDir.cdUp())
307 QString gpgIdPath(found ? gpgIdDir.absoluteFilePath(
".gpg-id")
320 if (!gpgId.open(QIODevice::ReadOnly | QIODevice::Text))
321 return QStringList();
322 QStringList recipients;
323 while (!gpgId.atEnd()) {
324 QString recipient(gpgId.readLine());
325 recipient = recipient.trimmed();
326 if (!recipient.isEmpty())
327 recipients += recipient;
355 const quint32 max_mod_bound = (1 + ~bound) % bound;
358 randval = QRandomGenerator::system()->generate();
359 }
while (randval < max_mod_bound);
361 return randval % bound;
365 unsigned int length) {
367 for (
unsigned int i = 0; i < length; ++i) {
368 out.append(charset.at(
static_cast<int>(
Executes external commands for handleing password, git and other data.
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.
void execute(int id, const QString &app, const QStringList &args, bool readStdout, bool readStderr=true)
Executor::execute execute an app.
void setEnvironment(const QStringList &env)
Executor::setEnvironment set environment variables for executor processes.
void starting()
starting signal that is emited when process starts
QString generateRandomPassword(const QString &charset, unsigned int length)
void startingExecuteWrapper()
void GenerateGPGKeys(QString batch)
Pass::GenerateGPGKeys internal gpg keypair generator . .
void finishedCopy(const QString &, const QString &)
void finishedShow(const QString &)
void finishedRemove(const QString &, const QString &)
void finishedMove(const QString &, const QString &)
static QStringList getRecipientString(QString for_file, QString separator=" ", int *count=NULL)
Pass::getRecipientString formated string for use with GPG.
void critical(QString, QString)
void finishedGitInit(const QString &, const QString &)
Pass()
Pass::Pass wrapper for using either pass or the pass imitation.
void executeWrapper(PROCESS id, const QString &app, const QStringList &args, bool readStdout=true, bool readStderr=true)
static QStringList getRecipientList(QString for_file)
Pass::getRecipientList return list of gpg-id's to encrypt for.
void finishedInsert(const QString &, const QString &)
void finishedInit(const QString &, const QString &)
void finishedOtpGenerate(const QString &)
virtual QString Generate_b(unsigned int length, const QString &charset)
Pass::Generate use either pwgen or internal password generator.
static QString getGpgIdPath(QString for_file)
Pass::getGpgIdPath return gpgid file path for some file (folder).
void processErrorExit(int exitCode, const QString &err)
quint32 boundedRandom(quint32 bound)
void finishedGitPull(const QString &, const QString &)
void finishedGitPush(const QString &, const QString &)
QList< UserInfo > listKeys(QStringList keystrings, bool secret=false)
Pass::listKeys list users.
void updateEnv()
Pass::updateEnv update the execution environment (used when switching profiles)
virtual void finished(int id, int exitCode, const QString &out, const QString &err)
Pass::processFinished reemits specific signal based on what process has finished.
void finishedGenerateGPGKeys(const QString &, const QString &)
static QString getPassSigningKey(const QString &defaultValue=QVariant().toString())
static bool isUseSymbols(const bool &defaultValue=QVariant().toBool())
static bool isLessRandom(const bool &defaultValue=QVariant().toBool())
static bool isAvoidNumbers(const bool &defaultValue=QVariant().toBool())
static QString getPwgenExecutable(const QString &defaultValue=QVariant().toString())
static bool isUsePwgen(const bool &defaultValue=QVariant().toBool())
static bool isAvoidCapitals(const bool &defaultValue=QVariant().toBool())
static QString getGpgExecutable(const QString &defaultValue=QVariant().toString())
static QString getPassStore(const QString &defaultValue=QVariant().toString())
static QString getGpgHome(const QString &defaultValue=QVariant().toString())
Enumerators for configuration and runtime items.
Stores key info lines including validity, creation date and more.
QString key_id
UserInfo::key_id hexadecimal representation.
QDateTime created
UserInfo::created date/time key was created.
QString name
UserInfo::name full name.
char validity
UserInfo::validity GnuPG representation of validity http://git.gnupg.org/cgi-bin/gitweb....
QDateTime expiry
UserInfo::expiry date/time key expires.