-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add netrc export option #13381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add netrc export option #13381
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,100 @@ | ||
| /* | ||
| * Copyright (C) 2015 Florian Geyer <blueice@fobos.de> | ||
| * Copyright (C) 2015 Felix Geyer <debfx@fobos.de> | ||
| * Copyright (C) 2026 Efraim Flashner <efraim@flashner.co.il> | ||
| * | ||
| * 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 "NetrcExporter.h" | ||
|
|
||
| #include <QFile> | ||
|
|
||
| #include "core/Group.h" | ||
|
|
||
| bool NetrcExporter::exportDatabase(const QString& filename, const QSharedPointer<const Database>& db) | ||
| { | ||
| QFile file(filename); | ||
| if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { | ||
| m_error = file.errorString(); | ||
| return false; | ||
| } | ||
| return exportDatabase(&file, db); | ||
| } | ||
|
|
||
| bool NetrcExporter::exportDatabase(QIODevice* device, const QSharedPointer<const Database>& db) | ||
| { | ||
| if (device->write(exportGroup(db->rootGroup()).toUtf8()) == -1) { | ||
| m_error = device->errorString(); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| QString NetrcExporter::exportDatabase(const QSharedPointer<const Database>& db) | ||
| { | ||
| return exportGroup(db->rootGroup()); | ||
| } | ||
|
|
||
| QString NetrcExporter::errorString() const | ||
| { | ||
| return m_error; | ||
| } | ||
|
|
||
| QString NetrcExporter::exportGroup(const Group* group, QString groupPath) | ||
| { | ||
| QString response; | ||
|
|
||
| const QList<Entry*>& entryList = group->entries(); | ||
| for (const Entry* entry : entryList) { | ||
| QString line; | ||
|
|
||
| if (!(entry->title().isEmpty())) { | ||
| addColumn(line, "machine"); | ||
| addColumn(line, entry->title()); | ||
| addColumn(line, "login"); | ||
| addColumn(line, entry->username()); | ||
| addColumn(line, "password"); | ||
| addColumn(line, entry->password()); | ||
| line.append("\n"); | ||
| } | ||
|
|
||
| // Add it a second time using the URL | ||
| if (!(entry->url().isEmpty())) { | ||
| addColumn(line, "machine"); | ||
| addColumn(line, entry->url()); | ||
| addColumn(line, "login"); | ||
| addColumn(line, entry->username()); | ||
| addColumn(line, "password"); | ||
| addColumn(line, entry->password()); | ||
| line.append("\n"); | ||
| } | ||
|
|
||
| response.append(line); | ||
| } | ||
|
|
||
| const QList<Group*>& children = group->children(); | ||
| for (const Group* child : children) { | ||
| response.append(exportGroup(child, groupPath)); | ||
| } | ||
|
|
||
| return response; | ||
| } | ||
|
|
||
| void NetrcExporter::addColumn(QString& str, const QString& column) | ||
| { | ||
| str.append(QString(column).replace("\"", "\"\"")); | ||
| str.append("\t"); | ||
| } | ||
This file contains hidden or 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,44 @@ | ||
| /* | ||
| * Copyright (C) 2015 Florian Geyer <blueice@fobos.de> | ||
| * Copyright (C) 2015 Felix Geyer <debfx@fobos.de> | ||
| * | ||
| * 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 KEEPASSX_NETRCEXPORTER_H | ||
| #define KEEPASSX_NETRCEXPORTER_H | ||
|
|
||
| #include <QSharedPointer> | ||
| #include <QString> | ||
|
|
||
| class Database; | ||
| class Group; | ||
| class QIODevice; | ||
|
|
||
| class NetrcExporter | ||
| { | ||
| public: | ||
| bool exportDatabase(const QString& filename, const QSharedPointer<const Database>& db); | ||
| bool exportDatabase(QIODevice* device, const QSharedPointer<const Database>& db); | ||
| QString exportDatabase(const QSharedPointer<const Database>& db); | ||
| QString errorString() const; | ||
|
|
||
| private: | ||
| QString exportGroup(const Group* group, QString groupPath = QString()); | ||
| void addColumn(QString& str, const QString& column); | ||
|
|
||
| QString m_error; | ||
| }; | ||
|
|
||
| #endif // KEEPASSX_NETRCEXPORTER_H |
This file contains hidden or 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 hidden or 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,104 @@ | ||
| /* | ||
| * Copyright (C) 2015 Florian Geyer <blueice@fobos.de> | ||
| * Copyright (C) 2015 Felix Geyer <debfx@fobos.de> | ||
| * | ||
| * 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 "TestNetrcExporter.h" | ||
|
|
||
| #include <QBuffer> | ||
| #include <QTest> | ||
|
|
||
| #include "core/Group.h" | ||
| #include "core/Tools.h" | ||
| #include "core/Totp.h" | ||
| #include "crypto/Crypto.h" | ||
| #include "format/NetrcExporter.h" | ||
|
|
||
| QTEST_GUILESS_MAIN(TestNetrcExporter) | ||
|
|
||
| void TestNetrcExporter::init() | ||
| { | ||
| m_db = QSharedPointer<Database>::create(); | ||
| m_netrcExporter = QSharedPointer<NetrcExporter>::create(); | ||
| } | ||
|
|
||
| void TestNetrcExporter::initTestCase() | ||
| { | ||
| Crypto::init(); | ||
| } | ||
|
|
||
| void TestNetrcExporter::cleanup() | ||
| { | ||
| } | ||
|
|
||
| void TestNetrcExporter::testExport() | ||
| { | ||
| Group* groupRoot = m_db->rootGroup(); | ||
| auto* group = new Group(); | ||
| group->setName("Test Group Name"); | ||
| group->setParent(groupRoot); | ||
| auto* entry = new Entry(); | ||
| entry->setGroup(group); | ||
| entry->setTitle("Test Entry Title"); | ||
| entry->setUsername("Test Username"); | ||
| entry->setPassword("Test Password"); | ||
| entry->setUrl("http://test.url"); | ||
| entry->setNotes("Test Notes"); | ||
| entry->setTotp(Totp::createSettings("DFDF", Totp::DEFAULT_DIGITS, Totp::DEFAULT_STEP)); | ||
| entry->setIcon(5); | ||
|
|
||
| QBuffer buffer; | ||
| QVERIFY(buffer.open(QIODevice::ReadWrite)); | ||
| m_netrcExporter->exportDatabase(&buffer, m_db); | ||
| auto exported = QString::fromUtf8(buffer.buffer()); | ||
|
|
||
| QString expectedResult = QString() | ||
| .append("machine\tTest Entry Title\tlogin\tTest Username\tpassword\tTest Password\t\n" | ||
| "machine\thttp://test.url\tlogin\tTest Username\tpassword\tTest Password\t\n"); | ||
|
|
||
| QVERIFY(exported.startsWith(expectedResult)); | ||
| } | ||
|
|
||
| void TestNetrcExporter::testEmptyDatabase() | ||
| { | ||
| QBuffer buffer; | ||
| QVERIFY(buffer.open(QIODevice::ReadWrite)); | ||
| m_netrcExporter->exportDatabase(&buffer, m_db); | ||
|
|
||
| QCOMPARE(QString::fromUtf8(buffer.buffer().constData()), QString("")); | ||
| } | ||
|
|
||
| void TestNetrcExporter::testNestedGroups() | ||
| { | ||
| Group* groupRoot = m_db->rootGroup(); | ||
| auto* group = new Group(); | ||
| group->setName("Test Group Name"); | ||
| group->setParent(groupRoot); | ||
| auto* childGroup = new Group(); | ||
| childGroup->setName("Test Sub Group Name"); | ||
| childGroup->setParent(group); | ||
| auto* entry = new Entry(); | ||
| entry->setGroup(childGroup); | ||
| entry->setTitle("Test Entry Title"); | ||
|
|
||
| QBuffer buffer; | ||
| QVERIFY(buffer.open(QIODevice::ReadWrite)); | ||
| m_netrcExporter->exportDatabase(&buffer, m_db); | ||
| auto exported = QString::fromUtf8(buffer.buffer()); | ||
| QVERIFY(exported.startsWith( | ||
| QString() | ||
| .append(""))); | ||
| } |
This file contains hidden or 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,47 @@ | ||
| /* | ||
| * Copyright (C) 2015 Florian Geyer <blueice@fobos.de> | ||
| * Copyright (C) 2015 Felix Geyer <debfx@fobos.de> | ||
| * | ||
| * 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 KEEPASSX_TESTNETRCEXPORTER_H | ||
| #define KEEPASSX_TESTNETRCEXPORTER_H | ||
|
|
||
| #include <QObject> | ||
| #include <QSharedPointer> | ||
|
|
||
| class Database; | ||
| class NetrcExporter; | ||
|
|
||
| class TestNetrcExporter : public QObject | ||
| { | ||
| Q_OBJECT | ||
|
|
||
| public: | ||
|
|
||
| private slots: | ||
| void init(); | ||
| void initTestCase(); | ||
| void cleanup(); | ||
| void testExport(); | ||
| void testEmptyDatabase(); | ||
| void testNestedGroups(); | ||
|
|
||
| private: | ||
| QSharedPointer<Database> m_db; | ||
| QSharedPointer<NetrcExporter> m_netrcExporter; | ||
| }; | ||
|
|
||
| #endif // KEEPASSX_TESTNETRCEXPORTER_H |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure these persons did not have anything to do with your source and header files. Check them all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not always sure which copyright headers to keep and which to toss when I've started by copying another file and then modifying it.