Skip to content

Commit

Permalink
[CLI] Add a db-edit command (#8400)
Browse files Browse the repository at this point in the history
  • Loading branch information
louib authored Oct 5, 2022
1 parent b1e7c34 commit db98f11
Show file tree
Hide file tree
Showing 16 changed files with 529 additions and 52 deletions.
6 changes: 6 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ All pull requests must comply with the above requirements and with the [stylegui
Translations are managed on [Transifex](https://www.transifex.com/keepassxc/keepassxc/) which offers a web interface.
Please join an existing language team or request a new one if there is none.

If you open a Pull Request with new strings that require translations, you will need to run the following:
```
./release-tool i18n lupdate
```
This will make the new strings available for translation in Transifex.

## Styleguides

### Git branch strategy
Expand Down
21 changes: 17 additions & 4 deletions docs/man/keepassxc-cli.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ It provides the ability to query and modify the entries of a KeePass database, d
The key file will be created if the file that is referred to does not exist.
If both the key file and password are empty, no database will be created.

*db-edit* [_options_] <__database__>::
Edits a database.
When setting a key file, the key file will be created if the file that is referred to
does not exist.

*db-info* [_options_] <__database__>::
Show a database's information.

Expand Down Expand Up @@ -154,7 +159,7 @@ It provides the ability to query and modify the entries of a KeePass database, d
*--no-password*::
Deactivates the password key for the database.

*-y*, *--yubikey* <__slot__>::
*-y*, *--yubikey* <__slot[:serial]__>::
Specifies a yubikey slot for unlocking the database.
In a merge operation this option is used to specify the YubiKey slot for the first database.

Expand All @@ -177,7 +182,7 @@ It provides the ability to query and modify the entries of a KeePass database, d
*--no-password-from*::
Deactivates password key for the database to merge from.

*--yubikey-from* <__slot__>::
*--yubikey-from* <__slot[:serial]__>::
YubiKey slot for the second database.

*-s*, *--same-credentials*::
Expand Down Expand Up @@ -235,16 +240,24 @@ The same password generation options as documented for the generate command can
If a unique matching entry is found it will be copied to the clipboard.
If multiple entries are found they will be listed to refine the search. (no clip performed)

=== Create and Import options
*-k*, *--set-key-file* <__path__>::
=== Db-create, Db-edit and Import options
*--set-key-file* <__path__>::
Set the key file for the database.

*-p*, *--set-password*::
Set a password for the database.

=== Db-create, Import options
*-t*, *--decryption-time* <__time__>::
Target decryption time in MS for the database.

=== Db-edit options
*--unset-password* <__path__>::
Removes the password for the database.

*--unset-key-file* <__path__>::
Removes the key file for the database.

=== Show options
*-a*, *--attributes* <__attribute__>...::
Shows the named attributes.
Expand Down
53 changes: 53 additions & 0 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7871,6 +7871,59 @@ Kernel: %3 %4</source>
<source>Show all the attributes of the entry.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Edit a database.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Could not change the database key.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Database was not modified.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Successfully edited the database.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Loading the new key file failed: %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unset the password for the database.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unset the key file for the database.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cannot use %1 and %2 at the same time.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cannot remove all the keys from a database.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cannot remove password: The database does not have a password.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cannot remove file key: The database does not have a file key.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Found unexpected Key type %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Set the key file for the database.
This options is deprecated, use --set-key-file instead.</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>QtIOCompressor</name>
Expand Down
5 changes: 3 additions & 2 deletions src/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ set(cli_SOURCES
AttachmentRemove.cpp
Clip.cpp
Close.cpp
Create.cpp
Command.cpp
DatabaseCommand.cpp
DatabaseCreate.cpp
DatabaseEdit.cpp
DatabaseInfo.cpp
Diceware.cpp
Edit.cpp
Estimate.cpp
Expand All @@ -33,7 +35,6 @@ set(cli_SOURCES
Generate.cpp
Help.cpp
Import.cpp
Info.cpp
List.cpp
Merge.cpp
Move.cpp
Expand Down
10 changes: 6 additions & 4 deletions src/cli/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#include "AttachmentRemove.h"
#include "Clip.h"
#include "Close.h"
#include "Create.h"
#include "DatabaseCreate.h"
#include "DatabaseEdit.h"
#include "DatabaseInfo.h"
#include "Diceware.h"
#include "Edit.h"
#include "Estimate.h"
Expand All @@ -32,7 +34,6 @@
#include "Generate.h"
#include "Help.h"
#include "Import.h"
#include "Info.h"
#include "List.h"
#include "Merge.h"
#include "Move.h"
Expand Down Expand Up @@ -172,8 +173,9 @@ namespace Commands
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()));
s_commands.insert(QStringLiteral("db-info"), QSharedPointer<Command>(new Info()));
s_commands.insert(QStringLiteral("db-create"), QSharedPointer<Command>(new DatabaseCreate()));
s_commands.insert(QStringLiteral("db-edit"), QSharedPointer<Command>(new DatabaseEdit()));
s_commands.insert(QStringLiteral("db-info"), QSharedPointer<Command>(new DatabaseInfo()));
s_commands.insert(QStringLiteral("diceware"), QSharedPointer<Command>(new Diceware()));
s_commands.insert(QStringLiteral("edit"), QSharedPointer<Command>(new Edit()));
s_commands.insert(QStringLiteral("estimate"), QSharedPointer<Command>(new Estimate()));
Expand Down
47 changes: 30 additions & 17 deletions src/cli/Create.cpp → src/cli/DatabaseCreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,47 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "Create.h"
#include "DatabaseCreate.h"

#include "Utils.h"
#include "keys/FileKey.h"

#include <QCommandLineParser>
#include <QFileInfo>

const QCommandLineOption Create::DecryptionTimeOption =
const QCommandLineOption DatabaseCreate::DecryptionTimeOption =
QCommandLineOption(QStringList() << "t"
<< "decryption-time",
QObject::tr("Target decryption time in MS for the database."),
QObject::tr("time"));

const QCommandLineOption Create::SetKeyFileOption =
QCommandLineOption(QStringList() << "k"
<< "set-key-file",
const QCommandLineOption DatabaseCreate::SetKeyFileShortOption = QCommandLineOption(
QStringList() << "k",
QObject::tr("Set the key file for the database.\nThis options is deprecated, use --set-key-file instead."),
QObject::tr("path"));

const QCommandLineOption DatabaseCreate::SetKeyFileOption =
QCommandLineOption(QStringList() << "set-key-file",
QObject::tr("Set the key file for the database."),
QObject::tr("path"));

const QCommandLineOption Create::SetPasswordOption =
const QCommandLineOption DatabaseCreate::SetPasswordOption =
QCommandLineOption(QStringList() << "p"
<< "set-password",
QObject::tr("Set a password for the database."));

Create::Create()
DatabaseCreate::DatabaseCreate()
{
name = QString("db-create");
description = QObject::tr("Create a new database.");
positionalArguments.append({QString("database"), QObject::tr("Path of the database."), QString("")});
options.append(Create::SetKeyFileOption);
options.append(Create::SetPasswordOption);
options.append(Create::DecryptionTimeOption);
options.append(DatabaseCreate::SetKeyFileOption);
options.append(DatabaseCreate::SetKeyFileShortOption);
options.append(DatabaseCreate::SetPasswordOption);
options.append(DatabaseCreate::DecryptionTimeOption);
}

QSharedPointer<Database> Create::initializeDatabaseFromOptions(const QSharedPointer<QCommandLineParser>& parser)
QSharedPointer<Database> DatabaseCreate::initializeDatabaseFromOptions(const QSharedPointer<QCommandLineParser>& parser)
{
if (parser.isNull()) {
return {};
Expand All @@ -60,7 +65,7 @@ QSharedPointer<Database> Create::initializeDatabaseFromOptions(const QSharedPoin
auto& err = Utils::STDERR;

// Validate the decryption time before asking for a password.
QString decryptionTimeValue = parser->value(Create::DecryptionTimeOption);
QString decryptionTimeValue = parser->value(DatabaseCreate::DecryptionTimeOption);
int decryptionTime = 0;
if (decryptionTimeValue.length() != 0) {
decryptionTime = decryptionTimeValue.toInt();
Expand All @@ -78,7 +83,7 @@ QSharedPointer<Database> Create::initializeDatabaseFromOptions(const QSharedPoin

auto key = QSharedPointer<CompositeKey>::create();

if (parser->isSet(Create::SetPasswordOption)) {
if (parser->isSet(DatabaseCreate::SetPasswordOption)) {
auto passwordKey = Utils::getConfirmedPassword();
if (passwordKey.isNull()) {
err << QObject::tr("Failed to set database password.") << endl;
Expand All @@ -87,10 +92,18 @@ QSharedPointer<Database> Create::initializeDatabaseFromOptions(const QSharedPoin
key->addKey(passwordKey);
}

if (parser->isSet(Create::SetKeyFileOption)) {
if (parser->isSet(DatabaseCreate::SetKeyFileOption) || parser->isSet(DatabaseCreate::SetKeyFileShortOption)) {
QSharedPointer<FileKey> fileKey;

if (!Utils::loadFileKey(parser->value(Create::SetKeyFileOption), fileKey)) {
QString keyFilePath;
if (parser->isSet(DatabaseCreate::SetKeyFileShortOption)) {
qWarning("The -k option will be deprecated. Please use the --set-key-file option instead.");
keyFilePath = parser->value(DatabaseCreate::SetKeyFileShortOption);
} else {
keyFilePath = parser->value(DatabaseCreate::SetKeyFileOption);
}

if (!Utils::loadFileKey(keyFilePath, fileKey)) {
err << QObject::tr("Loading the key file failed") << endl;
return {};
}
Expand Down Expand Up @@ -141,7 +154,7 @@ QSharedPointer<Database> Create::initializeDatabaseFromOptions(const QSharedPoin
*
* @return EXIT_SUCCESS on success, or EXIT_FAILURE on failure
*/
int Create::execute(const QStringList& arguments)
int DatabaseCreate::execute(const QStringList& arguments)
{
QSharedPointer<QCommandLineParser> parser = getCommandLineParser(arguments);
if (parser.isNull()) {
Expand All @@ -159,7 +172,7 @@ int Create::execute(const QStringList& arguments)
return EXIT_FAILURE;
}

QSharedPointer<Database> db = Create::initializeDatabaseFromOptions(parser);
QSharedPointer<Database> db = DatabaseCreate::initializeDatabaseFromOptions(parser);
if (!db) {
return EXIT_FAILURE;
}
Expand Down
11 changes: 6 additions & 5 deletions src/cli/Create.h → src/cli/DatabaseCreate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef KEEPASSXC_CREATE_H
#define KEEPASSXC_CREATE_H
#ifndef KEEPASSXC_DATABASECREATE_H
#define KEEPASSXC_DATABASECREATE_H

#include "Command.h"

class Create : public Command
class DatabaseCreate : public Command
{
public:
Create();
DatabaseCreate();
int execute(const QStringList& arguments) override;

static QSharedPointer<Database> initializeDatabaseFromOptions(const QSharedPointer<QCommandLineParser>& parser);

static const QCommandLineOption SetKeyFileOption;
static const QCommandLineOption SetKeyFileShortOption;
static const QCommandLineOption SetPasswordOption;
static const QCommandLineOption DecryptionTimeOption;
};

#endif // KEEPASSXC_CREATE_H
#endif // KEEPASSXC_DATABASECREATE_H
Loading

0 comments on commit db98f11

Please sign in to comment.