Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/cli/Show.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ const QCommandLineOption Show::AttributesOption = QCommandLineOption(
"If no attributes are specified, a summary of the default attributes is given."),
QObject::tr("attribute"));

const QCommandLineOption Show::NetrcOption =
QCommandLineOption(QStringList() << "format-netrc",
QObject::tr("Show a .netrc formatted output of the entry. Note that this option implies --show-protected."));

Show::Show()
{
name = QString("show");
Expand All @@ -53,6 +57,7 @@ Show::Show()
options.append(Show::ProtectedAttributesOption);
options.append(Show::AllAttributesOption);
options.append(Show::AttachmentsOption);
options.append(Show::NetrcOption);
positionalArguments.append({QString("entry"), QObject::tr("Name of the entry to show."), QString("")});
}

Expand All @@ -66,6 +71,7 @@ int Show::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
bool showTotp = parser->isSet(Show::TotpOption);
bool showProtectedAttributes = parser->isSet(Show::ProtectedAttributesOption);
bool showAllAttributes = parser->isSet(Show::AllAttributesOption);
bool showNetrcFormat = parser->isSet(Show::NetrcOption);
QStringList attributes = parser->values(Show::AttributesOption);

Entry* entry = database->rootGroup()->findEntryByPath(entryPath);
Expand All @@ -80,7 +86,11 @@ int Show::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
}

bool attributesWereSpecified = true;
if (showAllAttributes) {
if (showNetrcFormat) {
attributes = QStringList() << EntryAttributes::URLKey
<< EntryAttributes::UserNameKey
<< EntryAttributes::PasswordKey;
} else if (showAllAttributes) {
attributesWereSpecified = false;
attributes = EntryAttributes::DefaultAttributes;
for (QString fieldName : Utils::EntryFieldNames) {
Expand Down Expand Up @@ -129,15 +139,27 @@ int Show::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
QString canonicalName = attrs[0];
if (!attributesWereSpecified) {
out << canonicalName << ": ";
} else if (showNetrcFormat) {
QString netrcField = canonicalName;
if (canonicalName == "URL") {
netrcField = QString("machine");
} else if (canonicalName == "UserName") {
netrcField = QString("login");
} else if (canonicalName == "Password") {
netrcField = QString("password");
}
out << netrcField << " ";
}
if (entry->attributes()->isProtected(canonicalName) && !attributesWereSpecified && !showProtectedAttributes) {
out << "PROTECTED" << Qt::endl;
} else if (showNetrcFormat) {
out << entry->resolveMultiplePlaceholders(entry->attributes()->value(canonicalName)) << QString(" ");
} else {
out << entry->resolveMultiplePlaceholders(entry->attributes()->value(canonicalName)) << Qt::endl;
}
}

if (parser->isSet(Show::AttachmentsOption)) {
if (parser->isSet(Show::AttachmentsOption) && !showNetrcFormat) {
// Separate attachment output from attributes output via a newline.
out << Qt::endl;

Expand All @@ -156,9 +178,12 @@ int Show::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
}
}

if (showTotp) {
if (showTotp && !showNetrcFormat) {
out << entry->totp() << Qt::endl;
}
if (showNetrcFormat) {
out << Qt::endl;
}

return encounteredError ? EXIT_FAILURE : EXIT_SUCCESS;
}
1 change: 1 addition & 0 deletions src/cli/Show.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Show : public DatabaseCommand
static const QCommandLineOption AttributesOption;
static const QCommandLineOption ProtectedAttributesOption;
static const QCommandLineOption AttachmentsOption;
static const QCommandLineOption NetrcOption;
};

#endif // KEEPASSXC_SHOW_H
36 changes: 36 additions & 0 deletions tests/TestCli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,42 @@ void TestCli::testShow()
"TOTP Settings: 30;6\n"
"TestAttribute1: b\n"
"testattribute1: a\n"));

// Netrc formatted output shouldn't change regardless of extra options.
QByteArray expectedNetrcOutput = QByteArray("machine http://www.somesite.com/ "
"login User Name "
"password Password \n");

setInput("a");
execCmd(showCmd, {"show", "--format-netrc", m_dbFile->fileName(), "/Sample Entry"});
m_stderr->readLine(); // Skip password prompt
QCOMPARE(m_stderr->readAll(), QByteArray());
QCOMPARE(m_stdout->readAll(), expectedNetrcOutput);

setInput("a");
execCmd(showCmd, {"show", "-a", "DoesNotExist", "--format-netrc", m_dbFile->fileName(), "/Sample Entry"});
m_stderr->readLine(); // Skip password prompt
QCOMPARE(m_stderr->readAll(), QByteArray());
QCOMPARE(m_stdout->readAll(), expectedNetrcOutput);

setInput("a");
execCmd(showCmd, {"show", "--all", "--format-netrc", m_dbFile->fileName(), "/Sample Entry"});
m_stderr->readLine(); // Skip password prompt
QCOMPARE(m_stderr->readAll(), QByteArray());
QCOMPARE(m_stdout->readAll(), expectedNetrcOutput);

setInput("a");
execCmd(showCmd, {"show", "--totp", "--format-netrc", m_dbFile->fileName(), "/Sample Entry"});
m_stderr->readLine(); // Skip password prompt
QCOMPARE(m_stderr->readAll(), QByteArray());
QCOMPARE(m_stdout->readAll(), expectedNetrcOutput);

setInput("a");
execCmd(showCmd, {"show", "--format-netrc", "--show-attachments", m_dbFile->fileName(), "/Sample Entry"});
m_stderr->readLine(); // Skip password prompt
QCOMPARE(m_stderr->readAll(), QByteArray());
QCOMPARE(m_stdout->readAll(), expectedNetrcOutput);

}

void TestCli::testInvalidDbFiles()
Expand Down