Add -a --atribute, -A --atribute-value, -P, --unprotect for managing attributes in cli for command add or edit

This commit is contained in:
wolszewski 2022-02-24 09:52:13 +01:00
parent 4f0710350f
commit 87d25de586
5 changed files with 91 additions and 1 deletions

View File

@ -195,6 +195,20 @@ The same password generation options as documented for the generate command can
*--notes* <__notes__>::
Specifies the notes of the entry.
*-a*, *--attribute* <__attribute__>::
Specifies the attribute of the entry.
*-A*, *--attribute-value* <__attribute_value__>::
Specifies value of the new attribute for the entry.
Value must be base64 encoded.
*-P*, *--protect*::
Protect value of the new attribute for the entry.
*--unprotect*::
Unprotect value of the attribute for the entry.
By default new attributes are not protected.
*-p*, *--password-prompt*::
Uses a password prompt for the entry's password.

View File

@ -21,6 +21,7 @@
#include "Utils.h"
#include "core/Group.h"
#include "core/PasswordGenerator.h"
#include "core/Tools.h"
#include <QCommandLineParser>
@ -44,6 +45,15 @@ const QCommandLineOption Add::GenerateOption = QCommandLineOption(QStringList()
<< "generate",
QObject::tr("Generate a password for the entry."));
const QCommandLineOption Add::AttributeOption =
QCommandLineOption(QStringList() << "a" << "attribute", QObject::tr("Name of the attribute for the entry."), QObject::tr("attribute"));
const QCommandLineOption Add::AttributeValueOption =
QCommandLineOption(QStringList() << "A" << "attribute-value", QObject::tr("Value of the attribute for the entry."), QObject::tr("attribute-value"));
const QCommandLineOption Add::AttributeProtectOption =
QCommandLineOption(QStringList() << "P" << "protect", QObject::tr("Set the attribute to be protected."));
Add::Add()
{
name = QString("add");
@ -52,6 +62,9 @@ Add::Add()
options.append(Add::UrlOption);
options.append(Add::NotesOption);
options.append(Add::PasswordPromptOption);
options.append(Add::AttributeOption);
options.append(Add::AttributeValueOption);
options.append(Add::AttributeProtectOption);
positionalArguments.append({QString("entry"), QObject::tr("Path of the entry to add."), QString("")});
// Password generation options.
@ -120,6 +133,27 @@ int Add::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<Q
entry->setPassword(password);
}
if (!parser->value(Add::AttributeOption).isEmpty()) {
bool attributeProtect = false;
if(parser->isSet(Add::AttributeProtectOption)) {
attributeProtect = true;
}
QString attributeValue = "";
if(parser->isSet(Add::AttributeValueOption)) {
QByteArray qbaAttributeValue = parser->value(Add::AttributeValueOption).toUtf8();
if(!Tools::isBase64(qbaAttributeValue)){
err << QObject::tr("Attribute value is not base64 encoded.") << endl;
return EXIT_FAILURE;
}else{
attributeValue = QString( QByteArray::fromBase64(qbaAttributeValue) );
}
}
entry->attributes()->set(parser->value(Add::AttributeOption),attributeValue,attributeProtect);
}
QString errorMessage;
if (!database->save(Database::Atomic, {}, &errorMessage)) {
err << QObject::tr("Writing the database failed %1.").arg(errorMessage) << endl;

View File

@ -31,6 +31,9 @@ public:
static const QCommandLineOption UrlOption;
static const QCommandLineOption NotesOption;
static const QCommandLineOption PasswordPromptOption;
static const QCommandLineOption AttributeOption;
static const QCommandLineOption AttributeValueOption;
static const QCommandLineOption AttributeProtectOption;
static const QCommandLineOption GenerateOption;
};

View File

@ -22,6 +22,7 @@
#include "Utils.h"
#include "core/Group.h"
#include "core/PasswordGenerator.h"
#include "core/Tools.h"
#include <QCommandLineParser>
@ -29,6 +30,8 @@ const QCommandLineOption Edit::TitleOption = QCommandLineOption(QStringList() <<
<< "title",
QObject::tr("Title for the entry."),
QObject::tr("title"));
const QCommandLineOption Edit::AttributeUnprotectOption =
QCommandLineOption(QStringList() << "unprotect", QObject::tr("Set the attribute to be not protected."));
Edit::Edit()
{
@ -38,6 +41,10 @@ Edit::Edit()
options.append(Add::UsernameOption);
options.append(Add::UrlOption);
options.append(Add::NotesOption);
options.append(Add::AttributeOption);
options.append(Add::AttributeValueOption);
options.append(Add::AttributeProtectOption);
options.append(Edit::AttributeUnprotectOption);
options.append(Add::PasswordPromptOption);
options.append(Edit::TitleOption);
positionalArguments.append({QString("entry"), QObject::tr("Path of the entry to edit."), QString("")});
@ -90,11 +97,38 @@ int Edit::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
QString url = parser->value(Add::UrlOption);
QString notes = parser->value(Add::NotesOption);
QString title = parser->value(Edit::TitleOption);
QString attribute = parser->value(Add::AttributeOption);
bool prompt = parser->isSet(Add::PasswordPromptOption);
if (username.isEmpty() && url.isEmpty() && notes.isEmpty() && title.isEmpty() && !prompt && !generate) {
if (username.isEmpty() && url.isEmpty() && notes.isEmpty() && title.isEmpty() && attribute.isEmpty() && !prompt && !generate) {
err << QObject::tr("Not changing any field for entry %1.").arg(entryPath) << endl;
return EXIT_FAILURE;
}
bool attributeProtect = parser->isSet(Add::AttributeProtectOption);
bool attributeUnprotect = parser->isSet(Edit::AttributeUnprotectOption);
if (attributeProtect && attributeUnprotect){
err << QObject::tr("Protect and unprotect flag cannot be used together.") << endl;
return EXIT_FAILURE;
}
QString attributeValue = "";
if (!attribute.isEmpty()) {
if(!attributeProtect && !attributeUnprotect) {
attributeProtect = entry->attributes()->isProtected(attribute);
}
if(parser->isSet(Add::AttributeValueOption)) {
QByteArray qbaAttributeValue = parser->value(Add::AttributeValueOption).toUtf8();
if(!Tools::isBase64(qbaAttributeValue)){
err << QObject::tr("Attribute value is not base64 encoded.") << endl;
return EXIT_FAILURE;
}else{
attributeValue = QString( QByteArray::fromBase64(qbaAttributeValue) );
}
}else{
attributeValue = entry->attribute(attribute);
}
}
entry->beginUpdate();
@ -122,6 +156,10 @@ int Edit::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
QString password = passwordGenerator->generatePassword();
entry->setPassword(password);
}
if (!attribute.isEmpty()) {
entry->attributes()->set(attribute,attributeValue,attributeProtect);
}
entry->endUpdate();

View File

@ -27,6 +27,7 @@ public:
int executeWithDatabase(QSharedPointer<Database> db, QSharedPointer<QCommandLineParser> parser) override;
static const QCommandLineOption TitleOption;
static const QCommandLineOption AttributeUnprotectOption;
};
#endif // KEEPASSXC_EDIT_H