mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-08-05 04:54:20 -04:00
Add first keepassxc-cli version.
This commit is contained in:
parent
37441e3fac
commit
52991f3d66
7 changed files with 150 additions and 32 deletions
31
src/cli/CMakeLists.txt
Normal file
31
src/cli/CMakeLists.txt
Normal file
|
@ -0,0 +1,31 @@
|
|||
# Copyright (C) 2017 KeePassXC Team
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 2 or (at your option)
|
||||
# version 3 of the License.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
set(cli_SOURCES
|
||||
Merge.cpp
|
||||
Merge.h
|
||||
Extract.cpp
|
||||
Extract.h)
|
||||
|
||||
add_library(cli STATIC ${cli_SOURCES})
|
||||
target_link_libraries(cli Qt5::Core Qt5::Widgets)
|
||||
|
||||
add_executable(keepassxc-cli keepassxc-cli.cpp)
|
||||
target_link_libraries(keepassxc-cli
|
||||
cli
|
||||
keepassx_core
|
||||
Qt5::Core
|
||||
${GCRYPT_LIBRARIES}
|
||||
${ZLIB_LIBRARIES})
|
87
src/cli/Extract.cpp
Normal file
87
src/cli/Extract.cpp
Normal file
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 or (at your option)
|
||||
* version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "Extract.h"
|
||||
|
||||
#include <QCommandLineParser>
|
||||
#include <QCoreApplication>
|
||||
#include <QFile>
|
||||
#include <QStringList>
|
||||
#include <QTextStream>
|
||||
|
||||
#include "core/Database.h"
|
||||
#include "crypto/Crypto.h"
|
||||
#include "format/KeePass2Reader.h"
|
||||
#include "keys/CompositeKey.h"
|
||||
#include "keys/FileKey.h"
|
||||
#include "keys/PasswordKey.h"
|
||||
|
||||
int Extract::execute(int argc, char **argv)
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription(QCoreApplication::translate("main",
|
||||
"Extract and print a KeePassXC database file."));
|
||||
parser.addPositionalArgument("database", QCoreApplication::translate("main", "path of the database to extract."));
|
||||
parser.process(app);
|
||||
|
||||
const QStringList args = parser.positionalArguments();
|
||||
if (args.size() != 1) {
|
||||
parser.showHelp();
|
||||
return 1;
|
||||
}
|
||||
|
||||
static QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
|
||||
QString line = inputTextStream.readLine();
|
||||
CompositeKey key = CompositeKey::readFromLine(line);
|
||||
|
||||
QString databaseFilename = args.at(0);
|
||||
QFile dbFile(databaseFilename);
|
||||
if (!dbFile.exists()) {
|
||||
qCritical("File %s does not exist.", qPrintable(databaseFilename));
|
||||
return 1;
|
||||
}
|
||||
if (!dbFile.open(QIODevice::ReadOnly)) {
|
||||
qCritical("Unable to open file %s.", qPrintable(databaseFilename));
|
||||
return 1;
|
||||
}
|
||||
|
||||
KeePass2Reader reader;
|
||||
reader.setSaveXml(true);
|
||||
Database* db = reader.readDatabase(&dbFile, key);
|
||||
delete db;
|
||||
|
||||
QByteArray xmlData = reader.xmlData();
|
||||
|
||||
if (reader.hasError()) {
|
||||
if (xmlData.isEmpty()) {
|
||||
qCritical("Error while reading the database:\n%s", qPrintable(reader.errorString()));
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
qWarning("Error while parsing the database:\n%s\n", qPrintable(reader.errorString()));
|
||||
}
|
||||
}
|
||||
|
||||
QTextStream out(stdout);
|
||||
out << xmlData.constData() << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
27
src/cli/Extract.h
Normal file
27
src/cli/Extract.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (C) 2017 KeePassXC Team
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 or (at your option)
|
||||
* version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef KEEPASSXC_EXTRACT_H
|
||||
#define KEEPASSXC_EXTRACT_H
|
||||
|
||||
class Extract
|
||||
{
|
||||
public:
|
||||
static int execute(int argc, char** argv);
|
||||
};
|
||||
|
||||
#endif // KEEPASSXC_EXTRACT_H
|
132
src/cli/Merge.cpp
Normal file
132
src/cli/Merge.cpp
Normal file
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 or (at your option)
|
||||
* version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "Merge.h"
|
||||
|
||||
#include <QCommandLineParser>
|
||||
#include <QCoreApplication>
|
||||
#include <QFile>
|
||||
#include <QSaveFile>
|
||||
#include <QStringList>
|
||||
#include <QTextStream>
|
||||
|
||||
#include "core/Database.h"
|
||||
#include "format/KeePass2Reader.h"
|
||||
#include "format/KeePass2Writer.h"
|
||||
#include "keys/CompositeKey.h"
|
||||
|
||||
int Merge::execute(int argc, char** argv)
|
||||
{
|
||||
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription(QCoreApplication::translate("main", "Merge 2 KeePassXC database files."));
|
||||
parser.addPositionalArgument("database1", QCoreApplication::translate("main", "path of the database to merge into."));
|
||||
parser.addPositionalArgument("database2", QCoreApplication::translate("main", "path of the database to merge from."));
|
||||
|
||||
QCommandLineOption samePasswordOption(QStringList() << "s" << "same-password",
|
||||
QCoreApplication::translate("main", "use the same password for both database files."));
|
||||
|
||||
parser.addOption(samePasswordOption);
|
||||
parser.process(app);
|
||||
|
||||
const QStringList args = parser.positionalArguments();
|
||||
if (args.size() != 2) {
|
||||
parser.showHelp();
|
||||
return 1;
|
||||
}
|
||||
|
||||
static QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
|
||||
|
||||
QString line1 = inputTextStream.readLine();
|
||||
CompositeKey key1 = CompositeKey::readFromLine(line1);
|
||||
|
||||
CompositeKey key2;
|
||||
if (parser.isSet("same-password")) {
|
||||
key2 = *key1.clone();
|
||||
}
|
||||
else {
|
||||
QString line2 = inputTextStream.readLine();
|
||||
key2 = CompositeKey::readFromLine(line2);
|
||||
}
|
||||
|
||||
|
||||
QString databaseFilename1 = args.at(0);
|
||||
QFile dbFile1(databaseFilename1);
|
||||
if (!dbFile1.exists()) {
|
||||
qCritical("File %s does not exist.", qPrintable(databaseFilename1));
|
||||
return 1;
|
||||
}
|
||||
if (!dbFile1.open(QIODevice::ReadOnly)) {
|
||||
qCritical("Unable to open file %s.", qPrintable(databaseFilename1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
KeePass2Reader reader1;
|
||||
Database* db1 = reader1.readDatabase(&dbFile1, key1);
|
||||
|
||||
if (reader1.hasError()) {
|
||||
qCritical("Error while parsing the database:\n%s\n", qPrintable(reader1.errorString()));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
QString databaseFilename2 = args.at(1);
|
||||
QFile dbFile2(databaseFilename2);
|
||||
if (!dbFile2.exists()) {
|
||||
qCritical("File %s does not exist.", qPrintable(databaseFilename2));
|
||||
return 1;
|
||||
}
|
||||
if (!dbFile2.open(QIODevice::ReadOnly)) {
|
||||
qCritical("Unable to open file %s.", qPrintable(databaseFilename2));
|
||||
return 1;
|
||||
}
|
||||
|
||||
KeePass2Reader reader2;
|
||||
Database* db2 = reader2.readDatabase(&dbFile2, key2);
|
||||
|
||||
if (reader2.hasError()) {
|
||||
qCritical("Error while parsing the database:\n%s\n", qPrintable(reader2.errorString()));
|
||||
return 1;
|
||||
}
|
||||
|
||||
db1->merge(db2);
|
||||
|
||||
QSaveFile saveFile(databaseFilename1);
|
||||
if (!saveFile.open(QIODevice::WriteOnly)) {
|
||||
qCritical("Unable to open file %s for writing.", qPrintable(databaseFilename1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
KeePass2Writer writer;
|
||||
writer.writeDatabase(&saveFile, db1);
|
||||
|
||||
if (writer.hasError()) {
|
||||
qCritical("Error while updating the database:\n%s\n", qPrintable(writer.errorString()));
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!saveFile.commit()) {
|
||||
qCritical("Error while updating the database:\n%s\n", qPrintable(writer.errorString()));
|
||||
return 0;
|
||||
}
|
||||
|
||||
qDebug("Successfully merged the database files.\n");
|
||||
return 0;
|
||||
|
||||
}
|
27
src/cli/Merge.h
Normal file
27
src/cli/Merge.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (C) 2017 KeePassXC Team
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 or (at your option)
|
||||
* version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef KEEPASSXC_MERGE_H
|
||||
#define KEEPASSXC_MERGE_H
|
||||
|
||||
class Merge
|
||||
{
|
||||
public:
|
||||
static int execute(int argc, char** argv);
|
||||
};
|
||||
|
||||
#endif // KEEPASSXC_MERGE_H
|
77
src/cli/keepassxc-cli.cpp
Normal file
77
src/cli/keepassxc-cli.cpp
Normal file
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Copyright (C) 2017 KeePassXC Team
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 or (at your option)
|
||||
* version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cli/Merge.h>
|
||||
#include <cli/Extract.h>
|
||||
|
||||
#include <QCommandLineParser>
|
||||
#include <QCoreApplication>
|
||||
#include <QStringList>
|
||||
|
||||
#include "config-keepassx.h"
|
||||
#include "crypto/Crypto.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
if (!Crypto::init()) {
|
||||
qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString()));
|
||||
return 1;
|
||||
}
|
||||
|
||||
QCoreApplication app(argc, argv);
|
||||
app.setApplicationVersion(KEEPASSX_VERSION);
|
||||
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription(QCoreApplication::translate("main", "KeepassXC command line interface."));
|
||||
parser.addPositionalArgument("command", QCoreApplication::translate("main", "Name of the command to execute."));
|
||||
|
||||
parser.addHelpOption();
|
||||
parser.addVersionOption();
|
||||
parser.process(app);
|
||||
|
||||
const QStringList args = parser.positionalArguments();
|
||||
if (args.size() < 1) {
|
||||
parser.showHelp();
|
||||
return 1;
|
||||
}
|
||||
|
||||
QString commandName = args.at(0);
|
||||
|
||||
for (int i = 1; i < argc - 1; ++i) {
|
||||
argv[i] = argv[i+1];
|
||||
}
|
||||
argv[argc - 1] = nullptr;
|
||||
argc--;
|
||||
|
||||
if (commandName == "merge")
|
||||
{
|
||||
argv[0] = const_cast<char *>("keepassxc-cli merge");
|
||||
return Merge::execute(argc, argv);
|
||||
}
|
||||
|
||||
if (commandName == "extract")
|
||||
{
|
||||
argv[0] = const_cast<char *>("keepassxc-cli extract");
|
||||
return Extract::execute(argc, argv);
|
||||
}
|
||||
|
||||
qCritical("Invalid command %s.", qPrintable(commandName));
|
||||
parser.showHelp();
|
||||
return 1;
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue