-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add db statistic output to CLI db-info command.
Closes #6920
- Loading branch information
Showing
4 changed files
with
185 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* Copyright (C) 2021 KeePassXC Team <[email protected]> | ||
* | ||
* 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_DATABASESTATS_H | ||
#define KEEPASSXC_DATABASESTATS_H | ||
#include "PasswordHealth.h" | ||
#include "core/Group.h" | ||
#include <QFileInfo> | ||
#include <cmath> | ||
class DatabaseStats | ||
{ | ||
public: | ||
// The statistics we collect: | ||
QDateTime modified; // File modification time | ||
int groupCount = 0; // Number of groups in the database | ||
int entryCount = 0; // Number of entries (across all groups) | ||
int expiredEntries = 0; // Number of expired entries | ||
int excludedEntries = 0; // Number of known bad entries | ||
int weakPasswords = 0; // Number of weak or poor passwords | ||
int shortPasswords = 0; // Number of passwords 8 characters or less in size | ||
int uniquePasswords = 0; // Number of unique passwords | ||
int reusedPasswords = 0; // Number of non-unique passwords | ||
int totalPasswordLength = 0; // Total length of all passwords | ||
|
||
// Ctor does all the work | ||
explicit DatabaseStats(QSharedPointer<Database> db) | ||
: modified(QFileInfo(db->filePath()).lastModified()) | ||
, m_db(db) | ||
{ | ||
gatherStats(db->rootGroup()->groupsRecursive(true)); | ||
} | ||
|
||
// Get average password length | ||
int averagePwdLength() const | ||
{ | ||
const auto passwords = uniquePasswords + reusedPasswords; | ||
return passwords == 0 ? 0 : std::round(totalPasswordLength / double(passwords)); | ||
} | ||
|
||
// Get max number of password reuse (=how many entries | ||
// share the same password) | ||
int maxPwdReuse() const | ||
{ | ||
int ret = 0; | ||
for (const auto& count : m_passwords) { | ||
ret = std::max(ret, count); | ||
} | ||
return ret; | ||
} | ||
|
||
// A warning sign is displayed if one of the | ||
// following returns true. | ||
bool isAnyExpired() const | ||
{ | ||
return expiredEntries > 0; | ||
} | ||
|
||
bool areTooManyPwdsReused() const | ||
{ | ||
return reusedPasswords > uniquePasswords / 10; | ||
} | ||
|
||
bool arePwdsReusedTooOften() const | ||
{ | ||
return maxPwdReuse() > 3; | ||
} | ||
|
||
bool isAvgPwdTooShort() const | ||
{ | ||
return averagePwdLength() < 10; | ||
} | ||
|
||
private: | ||
QSharedPointer<Database> m_db; | ||
QHash<QString, int> m_passwords; | ||
|
||
void gatherStats(const QList<Group*>& groups) | ||
{ | ||
auto checker = HealthChecker(m_db); | ||
|
||
for (const auto* group : groups) { | ||
// Don't count anything in the recycle bin | ||
if (group->isRecycled()) { | ||
continue; | ||
} | ||
|
||
++groupCount; | ||
|
||
for (const auto* entry : group->entries()) { | ||
// Don't count anything in the recycle bin | ||
if (entry->isRecycled()) { | ||
continue; | ||
} | ||
|
||
++entryCount; | ||
|
||
if (entry->isExpired()) { | ||
++expiredEntries; | ||
} | ||
|
||
// Get password statistics | ||
const auto pwd = entry->password(); | ||
if (!pwd.isEmpty()) { | ||
if (!m_passwords.contains(pwd)) { | ||
++uniquePasswords; | ||
} else { | ||
++reusedPasswords; | ||
} | ||
|
||
if (pwd.size() < 8) { | ||
++shortPasswords; | ||
} | ||
|
||
// Speed up Zxcvbn process by excluding very long passwords and most passphrases | ||
if (pwd.size() < 25 && checker.evaluate(entry)->quality() <= PasswordHealth::Quality::Weak) { | ||
++weakPasswords; | ||
} | ||
|
||
if (entry->excludeFromReports()) { | ||
++excludedEntries; | ||
} | ||
|
||
totalPasswordLength += pwd.size(); | ||
m_passwords[pwd]++; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
#endif // KEEPASSXC_DATABASESTATS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters