Feature : Clip command using native programs. (#792)

* Adding a timeout option
* Using native apps.
* Renaming PasswordInput -> Utils
This commit is contained in:
louib 2017-07-22 19:40:30 -04:00 committed by GitHub
parent 90468e8095
commit 1edabc4b3c
7 changed files with 105 additions and 28 deletions

View File

@ -66,8 +66,8 @@ set(keepassx_SOURCES
core/Tools.cpp
core/Translator.cpp
core/Uuid.cpp
cli/PasswordInput.cpp
cli/PasswordInput.h
cli/Utils.cpp
cli/Utils.h
crypto/Crypto.cpp
crypto/CryptoHash.cpp
crypto/Random.cpp

View File

@ -15,8 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <chrono>
#include <cstdlib>
#include <stdio.h>
#include <thread>
#include "Clip.h"
@ -25,11 +27,11 @@
#include <QStringList>
#include <QTextStream>
#include "gui/UnlockDatabaseDialog.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "core/Entry.h"
#include "core/Group.h"
#include "gui/Clipboard.h"
#include "gui/UnlockDatabaseDialog.h"
Clip::Clip()
{
@ -50,6 +52,7 @@ int Clip::execute(int argc, char** argv)
}
QTextStream out(stdout);
QApplication app(argc, argv);
QCommandLineParser parser;
parser.setApplicationDescription(this->description);
@ -59,17 +62,19 @@ int Clip::execute(int argc, char** argv)
QObject::tr("Use a GUI prompt unlocking the database."));
parser.addOption(guiPrompt);
parser.addPositionalArgument("entry", QObject::tr("Path of the entry to clip."));
parser.addPositionalArgument(
"timeout",
QObject::tr("Timeout in seconds before clearing the clipboard."),
QString("[timeout]"));
parser.process(arguments);
const QStringList args = parser.positionalArguments();
if (args.size() != 2) {
QCoreApplication app(argc, argv);
if (args.size() != 2 && args.size() != 3) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli clip");
return EXIT_FAILURE;
}
Database* db = nullptr;
QApplication app(argc, argv);
if (parser.isSet("gui-prompt")) {
db = UnlockDatabaseDialog::openDatabasePrompt(args.at(0));
} else {
@ -79,12 +84,20 @@ int Clip::execute(int argc, char** argv)
if (!db) {
return EXIT_FAILURE;
}
return this->clipEntry(db, args.at(1));
return this->clipEntry(db, args.at(1), args.value(2));
}
int Clip::clipEntry(Database* database, QString entryPath)
int Clip::clipEntry(Database* database, QString entryPath, QString timeout)
{
int timeoutSeconds = 0;
if (!timeout.isEmpty() && !timeout.toInt()) {
qCritical("Invalid timeout value %s.", qPrintable(timeout));
return EXIT_FAILURE;
} else if (!timeout.isEmpty()) {
timeoutSeconds = timeout.toInt();
}
QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
Entry* entry = database->rootGroup()->findEntry(entryPath);
if (!entry) {
@ -92,7 +105,25 @@ int Clip::clipEntry(Database* database, QString entryPath)
return EXIT_FAILURE;
}
Clipboard::instance()->setText(entry->password());
return EXIT_SUCCESS;
int exitCode = Utils::clipText(entry->password());
if (exitCode != EXIT_SUCCESS) {
return exitCode;
}
outputTextStream << "Entry's password copied to the clipboard!" << endl;
if (!timeoutSeconds) {
return exitCode;
}
while (timeoutSeconds > 0) {
outputTextStream << "\rClearing the clipboard in " << timeoutSeconds << " seconds...";
outputTextStream.flush();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
timeoutSeconds--;
}
Utils::clipText("");
outputTextStream << "\nClipboard cleared!" << endl;
return EXIT_SUCCESS;
}

View File

@ -26,7 +26,7 @@ public:
Clip();
~Clip();
int execute(int argc, char** argv);
int clipEntry(Database* database, QString entryPath);
int clipEntry(Database* database, QString entryPath, QString timeout);
};
#endif // KEEPASSXC_CLIP_H

View File

@ -26,7 +26,7 @@
#include <QStringList>
#include <QTextStream>
#include "cli/PasswordInput.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "format/KeePass2Reader.h"
#include "keys/CompositeKey.h"
@ -66,7 +66,7 @@ int Extract::execute(int argc, char** argv)
out << "Insert the database password\n> ";
out.flush();
QString line = PasswordInput::getPassword();
QString line = Utils::getPassword();
CompositeKey key = CompositeKey::readFromLine(line);
QString databaseFilename = args.at(0);

View File

@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "PasswordInput.h"
#include "Utils.h"
#ifdef Q_OS_WIN
#include <windows.h>
@ -24,14 +24,11 @@
#include <unistd.h>
#endif
#include <QProcess>
#include <QTextStream>
PasswordInput::PasswordInput()
{
}
void PasswordInput::setStdinEcho(bool enable = true)
void Utils::setStdinEcho(bool enable = true)
{
#ifdef Q_OS_WIN
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
@ -60,7 +57,7 @@ void PasswordInput::setStdinEcho(bool enable = true)
#endif
}
QString PasswordInput::getPassword()
QString Utils::getPassword()
{
static QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
static QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
@ -75,3 +72,52 @@ QString PasswordInput::getPassword()
return line;
}
/*
* A valid and running event loop is needed to use the global QClipboard,
* so we need to use this from the CLI.
*/
int Utils::clipText(QString text)
{
QString programName = "";
QStringList arguments;
#ifdef Q_OS_UNIX
programName = "xclip";
arguments << "-i"
<< "-selection"
<< "clipboard";
#endif
#ifdef Q_OS_MACOS
programName = "pbcopy";
#endif
#ifdef Q_OS_WIN
programName = "clip";
#endif
if (programName.isEmpty()) {
qCritical("No program defined for clipboard manipulation");
return EXIT_FAILURE;
}
QProcess* clipProcess = new QProcess(nullptr);
clipProcess->start(programName, arguments);
clipProcess->waitForStarted();
if (clipProcess->state() != QProcess::Running) {
qCritical("Unable to start program %s", qPrintable(programName));
return EXIT_FAILURE;
}
if (clipProcess->write(text.toLatin1()) == -1) {
qDebug("Unable to write to process : %s", qPrintable(clipProcess->errorString()));
}
clipProcess->waitForBytesWritten();
clipProcess->closeWriteChannel();
clipProcess->waitForFinished();
return clipProcess->exitCode();
}

View File

@ -15,17 +15,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSXC_PASSWORDINPUT_H
#define KEEPASSXC_PASSWORDINPUT_H
#ifndef KEEPASSXC_UTILS_H
#define KEEPASSXC_UTILS_H
#include <QtCore/qglobal.h>
class PasswordInput
class Utils
{
public:
PasswordInput();
static void setStdinEcho(bool enable);
static QString getPassword();
static int clipText(QString text);
};
#endif // KEEPASSXC_PASSWORDINPUT_H
#endif // KEEPASSXC_UTILS_H

View File

@ -24,7 +24,7 @@
#include <QTimer>
#include <QXmlStreamReader>
#include "cli/PasswordInput.h"
#include "cli/Utils.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "crypto/Random.h"
@ -404,7 +404,7 @@ Database* Database::unlockFromStdin(QString databaseFilename)
outputTextStream << QString("Insert password to unlock " + databaseFilename + "\n> ");
outputTextStream.flush();
QString line = PasswordInput::getPassword();
QString line = Utils::getPassword();
CompositeKey key = CompositeKey::readFromLine(line);
return Database::openDatabaseFile(databaseFilename, key);
}