2017-01-06 16:25:26 -05:00
|
|
|
/*
|
2017-06-09 17:40:36 -04:00
|
|
|
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
2017-01-06 16:25:26 -05:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2017-02-02 17:30:54 -05:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2017-01-30 19:18:35 -05:00
|
|
|
#include "Merge.h"
|
2017-01-06 16:25:26 -05:00
|
|
|
|
2017-05-25 13:07:24 -04:00
|
|
|
#include <QApplication>
|
2017-01-11 21:00:11 -05:00
|
|
|
#include <QCommandLineParser>
|
2017-01-06 16:25:26 -05:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QSaveFile>
|
2017-01-06 20:24:50 -05:00
|
|
|
#include <QStringList>
|
|
|
|
#include <QTextStream>
|
2017-01-06 16:25:26 -05:00
|
|
|
|
|
|
|
#include "core/Database.h"
|
|
|
|
#include "format/KeePass2Writer.h"
|
2017-05-25 13:07:24 -04:00
|
|
|
#include "gui/UnlockDatabaseDialog.h"
|
2017-01-06 20:24:50 -05:00
|
|
|
|
2017-01-30 19:18:35 -05:00
|
|
|
int Merge::execute(int argc, char** argv)
|
2017-01-06 16:25:26 -05:00
|
|
|
{
|
2017-01-11 21:00:11 -05:00
|
|
|
|
2017-05-25 13:07:24 -04:00
|
|
|
QStringList arguments;
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
arguments << QString(argv[i]);
|
|
|
|
}
|
2017-02-16 14:26:51 -05:00
|
|
|
QTextStream out(stdout);
|
2017-01-06 16:25:26 -05:00
|
|
|
|
2017-01-11 21:00:11 -05:00
|
|
|
QCommandLineParser parser;
|
2017-02-15 21:05:40 -05:00
|
|
|
parser.setApplicationDescription(QCoreApplication::translate("main", "Merge two databases."));
|
2017-05-19 14:04:11 -04:00
|
|
|
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."));
|
2017-01-11 21:00:11 -05:00
|
|
|
|
2017-05-19 14:04:11 -04:00
|
|
|
QCommandLineOption samePasswordOption(
|
|
|
|
QStringList() << "s"
|
|
|
|
<< "same-password",
|
|
|
|
QCoreApplication::translate("main", "Use the same password for both database files."));
|
2017-01-11 21:00:11 -05:00
|
|
|
|
2017-05-25 13:07:24 -04:00
|
|
|
QCommandLineOption guiPrompt(
|
|
|
|
QStringList() << "g"
|
|
|
|
<< "gui-prompt",
|
|
|
|
QCoreApplication::translate("main", "Use a GUI prompt unlocking the database."));
|
|
|
|
parser.addOption(guiPrompt);
|
|
|
|
|
2017-01-11 21:00:11 -05:00
|
|
|
parser.addOption(samePasswordOption);
|
2017-05-25 13:07:24 -04:00
|
|
|
parser.process(arguments);
|
2017-01-11 21:00:11 -05:00
|
|
|
|
|
|
|
const QStringList args = parser.positionalArguments();
|
|
|
|
if (args.size() != 2) {
|
2017-05-25 13:07:24 -04:00
|
|
|
QCoreApplication app(argc, argv);
|
2017-01-11 21:00:11 -05:00
|
|
|
parser.showHelp();
|
2017-02-02 17:30:54 -05:00
|
|
|
return EXIT_FAILURE;
|
2017-01-06 16:25:26 -05:00
|
|
|
}
|
|
|
|
|
2017-05-25 13:07:24 -04:00
|
|
|
Database* db1;
|
|
|
|
Database* db2;
|
|
|
|
|
|
|
|
if (parser.isSet("gui-prompt")) {
|
|
|
|
QApplication app(argc, argv);
|
|
|
|
db1 = UnlockDatabaseDialog::openDatabasePrompt(args.at(0));
|
|
|
|
if (!parser.isSet("same-password")) {
|
|
|
|
db2 = UnlockDatabaseDialog::openDatabasePrompt(args.at(1));
|
|
|
|
} else {
|
|
|
|
db2 = Database::openDatabaseFile(args.at(1), *(db1->key().clone()));
|
|
|
|
}
|
2017-05-19 14:04:11 -04:00
|
|
|
} else {
|
2017-05-25 13:07:24 -04:00
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
db1 = Database::unlockFromStdin(args.at(0));
|
|
|
|
if (!parser.isSet("same-password")) {
|
|
|
|
db2 = Database::unlockFromStdin(args.at(1));
|
|
|
|
} else {
|
|
|
|
db2 = Database::openDatabaseFile(args.at(1), *(db1->key().clone()));
|
|
|
|
}
|
2017-01-06 16:25:26 -05:00
|
|
|
}
|
2017-03-12 13:56:30 -04:00
|
|
|
if (db1 == nullptr) {
|
2017-02-02 17:30:54 -05:00
|
|
|
return EXIT_FAILURE;
|
2017-01-06 16:25:26 -05:00
|
|
|
}
|
2017-03-12 13:56:30 -04:00
|
|
|
if (db2 == nullptr) {
|
2017-02-02 17:30:54 -05:00
|
|
|
return EXIT_FAILURE;
|
2017-01-06 16:25:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
db1->merge(db2);
|
|
|
|
|
2017-03-12 13:56:30 -04:00
|
|
|
QSaveFile saveFile(args.at(0));
|
2017-01-06 16:25:26 -05:00
|
|
|
if (!saveFile.open(QIODevice::WriteOnly)) {
|
2017-03-12 13:56:30 -04:00
|
|
|
qCritical("Unable to open file %s for writing.", qPrintable(args.at(0)));
|
2017-02-02 17:30:54 -05:00
|
|
|
return EXIT_FAILURE;
|
2017-01-06 16:25:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
KeePass2Writer writer;
|
|
|
|
writer.writeDatabase(&saveFile, db1);
|
|
|
|
|
|
|
|
if (writer.hasError()) {
|
|
|
|
qCritical("Error while updating the database:\n%s\n", qPrintable(writer.errorString()));
|
2017-02-02 17:30:54 -05:00
|
|
|
return EXIT_FAILURE;
|
2017-01-06 16:25:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!saveFile.commit()) {
|
|
|
|
qCritical("Error while updating the database:\n%s\n", qPrintable(writer.errorString()));
|
2017-02-02 17:30:54 -05:00
|
|
|
return EXIT_FAILURE;
|
2017-01-06 16:25:26 -05:00
|
|
|
}
|
|
|
|
|
2017-02-16 14:26:51 -05:00
|
|
|
out << "Successfully merged the database files.\n";
|
2017-02-02 17:30:54 -05:00
|
|
|
return EXIT_SUCCESS;
|
2017-01-06 16:25:26 -05:00
|
|
|
}
|