-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathShow.cpp
More file actions
189 lines (167 loc) · 7.83 KB
/
Copy pathShow.cpp
File metadata and controls
189 lines (167 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* 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 "Show.h"
#include "Utils.h"
#include "core/Group.h"
#include "core/Tools.h"
#include <QCommandLineParser>
const QCommandLineOption Show::TotpOption =
QCommandLineOption(QStringList() << "t" << "totp", QObject::tr("Show the entry's current TOTP."));
const QCommandLineOption Show::ProtectedAttributesOption =
QCommandLineOption(QStringList() << "s" << "show-protected",
QObject::tr("Show the protected attributes in clear text."));
const QCommandLineOption Show::AllAttributesOption =
QCommandLineOption(QStringList() << "all", QObject::tr("Show all the attributes of the entry."));
const QCommandLineOption Show::AttachmentsOption =
QCommandLineOption(QStringList() << "show-attachments", QObject::tr("Show the attachments of the entry."));
const QCommandLineOption Show::AttributesOption = QCommandLineOption(
QStringList() << "a" << "attributes",
QObject::tr(
"Names of the attributes to show. "
"This option can be specified more than once, with each attribute shown one-per-line in the given order. "
"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");
description = QObject::tr("Show an entry's information.");
options.append(Show::TotpOption);
options.append(Show::AttributesOption);
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("")});
}
int Show::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
{
auto& out = Utils::STDOUT;
auto& err = Utils::STDERR;
const QStringList args = parser->positionalArguments();
const QString& entryPath = args.at(1);
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);
if (!entry) {
err << QObject::tr("Could not find entry with path %1.").arg(entryPath) << Qt::endl;
return EXIT_FAILURE;
}
if (showTotp && !entry->hasTotp()) {
err << QObject::tr("Entry with path %1 has no TOTP set up.").arg(entryPath) << Qt::endl;
return EXIT_FAILURE;
}
bool attributesWereSpecified = true;
if (showNetrcFormat) {
attributes = QStringList() << EntryAttributes::URLKey
<< EntryAttributes::UserNameKey
<< EntryAttributes::PasswordKey;
} else if (showAllAttributes) {
attributesWereSpecified = false;
attributes = EntryAttributes::DefaultAttributes;
for (QString fieldName : Utils::EntryFieldNames) {
attributes.append(fieldName);
}
// Adding the custom attributes after the default attributes so that
// the default attributes are always shown first.
for (QString attributeName : entry->attributes()->keys()) {
if (EntryAttributes::DefaultAttributes.contains(attributeName)) {
continue;
}
attributes.append(attributeName);
}
} else if (attributes.isEmpty() && !showTotp) {
// If no attributes are specified, output the default attribute set.
attributesWereSpecified = false;
attributes = EntryAttributes::DefaultAttributes;
for (QString fieldName : Utils::EntryFieldNames) {
attributes.append(fieldName);
}
}
// Iterate over the attributes and output them line-by-line.
bool encounteredError = false;
for (const QString& attributeName : asConst(attributes)) {
if (Utils::EntryFieldNames.contains(attributeName)) {
if (!attributesWereSpecified) {
out << attributeName << ": ";
}
out << Utils::getTopLevelField(entry, attributeName) << Qt::endl;
continue;
}
QStringList attrs = Utils::findAttributes(*entry->attributes(), attributeName);
if (attrs.isEmpty()) {
encounteredError = true;
err << QObject::tr("ERROR: unknown attribute %1.").arg(attributeName) << Qt::endl;
continue;
} else if (attrs.size() > 1) {
encounteredError = true;
err << QObject::tr("ERROR: attribute %1 is ambiguous, it matches %2.")
.arg(attributeName, QLocale().createSeparatedList(attrs))
<< Qt::endl;
continue;
}
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) && !showNetrcFormat) {
// Separate attachment output from attributes output via a newline.
out << Qt::endl;
EntryAttachments* attachments = entry->attachments();
if (attachments->isEmpty()) {
out << QObject::tr("No attachments present.") << Qt::endl;
} else {
out << QObject::tr("Attachments:") << Qt::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 << ")" << Qt::endl;
}
}
}
if (showTotp && !showNetrcFormat) {
out << entry->totp() << Qt::endl;
}
if (showNetrcFormat) {
out << Qt::endl;
}
return encounteredError ? EXIT_FAILURE : EXIT_SUCCESS;
}