mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-08-06 13:34:16 -04:00
CLI: Add commands to handle attachments
* Add commands to manipulate entry attachments from the CLI * Closes #4462 * Add the following commands: attachment-export: Exports the content of an attachment to a specified file. attachment-import: Imports the attachment into an entry. An existing attachment with the same name may be overwritten if the -f option is specified. attachment-rm: Removes the named attachment from an entry. * Add --show-attachments to the show command
This commit is contained in:
parent
7811f10dba
commit
7d37f65ad0
16 changed files with 697 additions and 3 deletions
88
src/cli/AttachmentExport.cpp
Normal file
88
src/cli/AttachmentExport.cpp
Normal file
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* 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 "AttachmentExport.h"
|
||||
|
||||
#include "Utils.h"
|
||||
#include "core/Group.h"
|
||||
|
||||
#include <QCommandLineParser>
|
||||
#include <QFile>
|
||||
|
||||
const QCommandLineOption AttachmentExport::StdoutOption =
|
||||
QCommandLineOption(QStringList() << "stdout", QObject::tr(""));
|
||||
|
||||
AttachmentExport::AttachmentExport()
|
||||
{
|
||||
name = QString("attachment-export");
|
||||
description = QObject::tr("Export an attachment of an entry.");
|
||||
options.append(AttachmentExport::StdoutOption);
|
||||
positionalArguments.append(
|
||||
{QString("entry"), QObject::tr("Path of the entry with the target attachment."), QString("")});
|
||||
positionalArguments.append(
|
||||
{QString("attachment-name"), QObject::tr("Name of the attachment to be exported."), QString("")});
|
||||
optionalArguments.append(
|
||||
{QString("export-file"), QObject::tr("Path to which the attachment should be exported."), QString("")});
|
||||
}
|
||||
|
||||
int AttachmentExport::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
|
||||
{
|
||||
auto& out = parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT;
|
||||
auto& err = Utils::STDERR;
|
||||
|
||||
auto args = parser->positionalArguments();
|
||||
auto entryPath = args.at(1);
|
||||
|
||||
auto entry = database->rootGroup()->findEntryByPath(entryPath);
|
||||
if (!entry) {
|
||||
err << QObject::tr("Could not find entry with path %1.").arg(entryPath) << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
auto attachmentName = args.at(2);
|
||||
|
||||
auto attachments = entry->attachments();
|
||||
if (!attachments->hasKey(attachmentName)) {
|
||||
err << QObject::tr("Could not find attachment with name %1.").arg(attachmentName) << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (parser->isSet(AttachmentExport::StdoutOption)) {
|
||||
// Output to STDOUT even in quiet mode
|
||||
Utils::STDOUT << attachments->value(attachmentName) << flush;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
if (args.size() < 4) {
|
||||
err << QObject::tr("No export target given. Please use '--stdout' or specify an 'export-file'.") << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
auto exportFileName = args.at(3);
|
||||
QFile exportFile(exportFileName);
|
||||
if (!exportFile.open(QIODevice::WriteOnly)) {
|
||||
err << QObject::tr("Could not open output file %1.").arg(exportFileName) << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
exportFile.write(attachments->value(attachmentName));
|
||||
|
||||
out << QObject::tr("Successfully exported attachment %1 of entry %2 to %3.")
|
||||
.arg(attachmentName, entryPath, exportFileName)
|
||||
<< endl;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
33
src/cli/AttachmentExport.h
Normal file
33
src/cli/AttachmentExport.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* 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_ATTACHMENTEXPORT_H
|
||||
#define KEEPASSXC_ATTACHMENTEXPORT_H
|
||||
|
||||
#include "DatabaseCommand.h"
|
||||
|
||||
class AttachmentExport : public DatabaseCommand
|
||||
{
|
||||
public:
|
||||
AttachmentExport();
|
||||
|
||||
int executeWithDatabase(QSharedPointer<Database> db, QSharedPointer<QCommandLineParser> parser) override;
|
||||
|
||||
static const QCommandLineOption StdoutOption;
|
||||
};
|
||||
|
||||
#endif // KEEPASSXC_ATTACHMENTEXPORT_H
|
87
src/cli/AttachmentImport.cpp
Normal file
87
src/cli/AttachmentImport.cpp
Normal file
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* 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 "AttachmentImport.h"
|
||||
|
||||
#include "Utils.h"
|
||||
#include "core/Group.h"
|
||||
|
||||
#include <QCommandLineParser>
|
||||
#include <QFile>
|
||||
|
||||
const QCommandLineOption AttachmentImport::ForceOption =
|
||||
QCommandLineOption(QStringList() << "f"
|
||||
<< "force",
|
||||
QObject::tr("Overwrite existing attachments."));
|
||||
|
||||
AttachmentImport::AttachmentImport()
|
||||
{
|
||||
name = QString("attachment-import");
|
||||
description = QObject::tr("Imports an attachment to an entry.");
|
||||
options.append(AttachmentImport::ForceOption);
|
||||
positionalArguments.append({QString("entry"), QObject::tr("Path of the entry."), QString("")});
|
||||
positionalArguments.append(
|
||||
{QString("attachment-name"), QObject::tr("Name of the attachment to be added."), QString("")});
|
||||
positionalArguments.append(
|
||||
{QString("import-file"), QObject::tr("Path of the attachment to be imported."), QString("")});
|
||||
}
|
||||
|
||||
int AttachmentImport::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
|
||||
{
|
||||
auto& out = parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT;
|
||||
auto& err = Utils::STDERR;
|
||||
|
||||
auto args = parser->positionalArguments();
|
||||
auto entryPath = args.at(1);
|
||||
|
||||
auto entry = database->rootGroup()->findEntryByPath(entryPath);
|
||||
if (!entry) {
|
||||
err << QObject::tr("Could not find entry with path %1.").arg(entryPath) << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
auto attachmentName = args.at(2);
|
||||
|
||||
auto attachments = entry->attachments();
|
||||
if (attachments->hasKey(attachmentName) && !parser->isSet(AttachmentImport::ForceOption)) {
|
||||
err << QObject::tr("Attachment %1 already exists for entry %2.").arg(attachmentName, entryPath) << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
auto importFileName = args.at(3);
|
||||
|
||||
QFile importFile(importFileName);
|
||||
if (!importFile.open(QIODevice::ReadOnly)) {
|
||||
err << QObject::tr("Could not open attachment file %1.").arg(importFileName) << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
entry->beginUpdate();
|
||||
attachments->set(attachmentName, importFile.readAll());
|
||||
entry->endUpdate();
|
||||
|
||||
QString errorMessage;
|
||||
if (!database->save(Database::Atomic, false, &errorMessage)) {
|
||||
err << QObject::tr("Writing the database failed %1.").arg(errorMessage) << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
out << QObject::tr("Successfully imported attachment %1 as %2 to entry %3.")
|
||||
.arg(importFileName, attachmentName, entryPath)
|
||||
<< endl;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
33
src/cli/AttachmentImport.h
Normal file
33
src/cli/AttachmentImport.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* 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_ATTACHMENTIMPORT_H
|
||||
#define KEEPASSXC_ATTACHMENTIMPORT_H
|
||||
|
||||
#include "DatabaseCommand.h"
|
||||
|
||||
class AttachmentImport : public DatabaseCommand
|
||||
{
|
||||
public:
|
||||
AttachmentImport();
|
||||
|
||||
int executeWithDatabase(QSharedPointer<Database> db, QSharedPointer<QCommandLineParser> parser) override;
|
||||
|
||||
static const QCommandLineOption ForceOption;
|
||||
};
|
||||
|
||||
#endif // KEEPASSXC_ATTACHMENTIMPORT_H
|
68
src/cli/AttachmentRemove.cpp
Normal file
68
src/cli/AttachmentRemove.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* 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 "AttachmentRemove.h"
|
||||
|
||||
#include "Utils.h"
|
||||
#include "core/Group.h"
|
||||
|
||||
#include <QCommandLineParser>
|
||||
|
||||
AttachmentRemove::AttachmentRemove()
|
||||
{
|
||||
name = QString("attachment-rm");
|
||||
description = QObject::tr("Remove an attachment of an entry.");
|
||||
positionalArguments.append(
|
||||
{QString("entry"), QObject::tr("Path of the entry with the target attachment."), QString("")});
|
||||
positionalArguments.append({QString("name"), QObject::tr("Name of the attachment to be removed."), QString("")});
|
||||
}
|
||||
|
||||
int AttachmentRemove::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
|
||||
{
|
||||
auto& out = parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT;
|
||||
auto& err = Utils::STDERR;
|
||||
|
||||
auto args = parser->positionalArguments();
|
||||
auto entryPath = args.at(1);
|
||||
|
||||
auto entry = database->rootGroup()->findEntryByPath(entryPath);
|
||||
if (!entry) {
|
||||
err << QObject::tr("Could not find entry with path %1.").arg(entryPath) << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
auto attachmentName = args.at(2);
|
||||
|
||||
auto attachments = entry->attachments();
|
||||
if (!attachments->hasKey(attachmentName)) {
|
||||
err << QObject::tr("Could not find attachment with name %1.").arg(attachmentName) << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
entry->beginUpdate();
|
||||
attachments->remove(attachmentName);
|
||||
entry->endUpdate();
|
||||
|
||||
QString errorMessage;
|
||||
if (!database->save(Database::Atomic, false, &errorMessage)) {
|
||||
err << QObject::tr("Writing the database failed %1.").arg(errorMessage) << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
out << QObject::tr("Successfully removed attachment %1 from entry %2.").arg(attachmentName, entryPath) << endl;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
31
src/cli/AttachmentRemove.h
Normal file
31
src/cli/AttachmentRemove.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* 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_ATTACHMENTREMOVE_H
|
||||
#define KEEPASSXC_ATTACHMENTREMOVE_H
|
||||
|
||||
#include "DatabaseCommand.h"
|
||||
|
||||
class AttachmentRemove : public DatabaseCommand
|
||||
{
|
||||
public:
|
||||
AttachmentRemove();
|
||||
|
||||
int executeWithDatabase(QSharedPointer<Database> db, QSharedPointer<QCommandLineParser> parser) override;
|
||||
};
|
||||
|
||||
#endif // KEEPASSXC_ATTACHMENTMOVE_H
|
|
@ -17,6 +17,9 @@ set(cli_SOURCES
|
|||
Add.cpp
|
||||
AddGroup.cpp
|
||||
Analyze.cpp
|
||||
AttachmentExport.cpp
|
||||
AttachmentImport.cpp
|
||||
AttachmentRemove.cpp
|
||||
Clip.cpp
|
||||
Close.cpp
|
||||
Create.cpp
|
||||
|
|
|
@ -18,6 +18,9 @@
|
|||
#include "Add.h"
|
||||
#include "AddGroup.h"
|
||||
#include "Analyze.h"
|
||||
#include "AttachmentExport.h"
|
||||
#include "AttachmentImport.h"
|
||||
#include "AttachmentRemove.h"
|
||||
#include "Clip.h"
|
||||
#include "Close.h"
|
||||
#include "Create.h"
|
||||
|
@ -107,7 +110,7 @@ QString Command::getDescriptionLine()
|
|||
{
|
||||
QString response = name;
|
||||
QString space(" ");
|
||||
QString spaces = space.repeated(15 - name.length());
|
||||
QString spaces = space.repeated(20 - name.length());
|
||||
response = response.append(spaces);
|
||||
response = response.append(description);
|
||||
response = response.append("\n");
|
||||
|
@ -164,6 +167,9 @@ namespace Commands
|
|||
|
||||
s_commands.insert(QStringLiteral("add"), QSharedPointer<Command>(new Add()));
|
||||
s_commands.insert(QStringLiteral("analyze"), QSharedPointer<Command>(new Analyze()));
|
||||
s_commands.insert(QStringLiteral("attachment-export"), QSharedPointer<Command>(new AttachmentExport()));
|
||||
s_commands.insert(QStringLiteral("attachment-import"), QSharedPointer<Command>(new AttachmentImport()));
|
||||
s_commands.insert(QStringLiteral("attachment-rm"), QSharedPointer<Command>(new AttachmentRemove()));
|
||||
s_commands.insert(QStringLiteral("clip"), QSharedPointer<Command>(new Clip()));
|
||||
s_commands.insert(QStringLiteral("close"), QSharedPointer<Command>(new Close()));
|
||||
s_commands.insert(QStringLiteral("db-create"), QSharedPointer<Command>(new Create()));
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
#include "Utils.h"
|
||||
#include "core/Group.h"
|
||||
#include "core/Tools.h"
|
||||
|
||||
#include <QCommandLineParser>
|
||||
|
||||
|
@ -31,6 +32,9 @@ const QCommandLineOption Show::ProtectedAttributesOption =
|
|||
<< "show-protected",
|
||||
QObject::tr("Show the protected attributes in clear text."));
|
||||
|
||||
const QCommandLineOption Show::AttachmentsOption =
|
||||
QCommandLineOption(QStringList() << "show-attachments", QObject::tr("Show the attachments of the entry."));
|
||||
|
||||
const QCommandLineOption Show::AttributesOption = QCommandLineOption(
|
||||
QStringList() << "a"
|
||||
<< "attributes",
|
||||
|
@ -47,6 +51,7 @@ Show::Show()
|
|||
options.append(Show::TotpOption);
|
||||
options.append(Show::AttributesOption);
|
||||
options.append(Show::ProtectedAttributesOption);
|
||||
options.append(Show::AttachmentsOption);
|
||||
positionalArguments.append({QString("entry"), QObject::tr("Name of the entry to show."), QString("")});
|
||||
}
|
||||
|
||||
|
@ -104,6 +109,25 @@ int Show::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
|
|||
}
|
||||
}
|
||||
|
||||
if (parser->isSet(Show::AttachmentsOption)) {
|
||||
// Separate attachment output from attributes output via a newline.
|
||||
out << endl;
|
||||
|
||||
EntryAttachments* attachments = entry->attachments();
|
||||
if (attachments->isEmpty()) {
|
||||
out << QObject::tr("No attachments present.") << endl;
|
||||
} else {
|
||||
out << QObject::tr("Attachments:") << endl;
|
||||
|
||||
// Iterate over the attachments and output their names and size line-by-line, indented.
|
||||
for (const QString& attachmentName : attachments->keys()) {
|
||||
// TODO: use QLocale::formattedDataSize when >= Qt 5.10
|
||||
QString attachmentSize = Tools::humanReadableFileSize(attachments->value(attachmentName).size(), 1);
|
||||
out << " " << attachmentName << " (" << attachmentSize << ")" << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (showTotp) {
|
||||
out << entry->totp() << endl;
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ public:
|
|||
static const QCommandLineOption TotpOption;
|
||||
static const QCommandLineOption AttributesOption;
|
||||
static const QCommandLineOption ProtectedAttributesOption;
|
||||
static const QCommandLineOption AttachmentsOption;
|
||||
};
|
||||
|
||||
#endif // KEEPASSXC_SHOW_H
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue