Git commit 9ce3f9ce184332cfe46d6fbf543a8f2aa4de30ec by Andrea Scarpino. Committed on 13/06/2015 at 09:11. Pushed by vrusu into branch 'master'.
Support for reading every folder in the wallet. A new -f, --folder option is provided to set the folder name, by default "Passwords" is used. Some folder stores map entries instead of passwords, this required a new method to read and display maps. M +20 -10 doc/man-kwallet-query.1.docbook M +5 -0 src/main.cpp M +46 -16 src/querydriver.cpp M +5 -1 src/querydriver.h http://commits.kde.org/kwallet-framework/9ce3f9ce184332cfe46d6fbf543a8f2aa4de30ec diff --git a/doc/man-kwallet-query.1.docbook b/doc/man-kwallet-query.1.docbook index 9cddd13..3b70926 100644 --- a/doc/man-kwallet-query.1.docbook +++ b/doc/man-kwallet-query.1.docbook @@ -21,11 +21,11 @@ </refsynopsisdiv> <refsect1 id="_description"> <title>DESCRIPTION</title> -<simpara>kwallet-query comes in handy when shell scripts need to read or update the KDE -Wallet. It works by manipulating the entries under the <emphasis role="strong">Passwords</emphasis> section, as -displayed in the <emphasis role="strong">KDE Wallet Manager</emphasis> utility. It’s only parameter is the -<emphasis>wallet</emphasis> name the tool should read or update. The operation mode is specified -by the options.</simpara> +<simpara> + kwallet-query comes in handy when shell scripts need to read or update the KDE Wallet. It works by manipulating the entries + displayed in the <emphasis role="strong">KDE Wallet Manager</emphasis> utility. It’s only parameter is the + <emphasis>wallet</emphasis> name the tool should read or update. The operation mode is specified by the options. +</simpara> </refsect1> <refsect1 id="_options"> <title>OPTIONS</title> @@ -46,8 +46,8 @@ by the options.</simpara> </term> <listitem> <simpara> - List password entries. These are the folder names under the <emphasis role="strong">Passwords</emphasis> - section when the <emphasis>wallet</emphasis> is displayed in the <emphasis role="strong">KDE Wallet Manager</emphasis> utility. + List password entries. These are the folder names displayed in the <emphasis role="strong">KDE Wallet Manager</emphasis> + utility. </simpara> </listitem> </varlistentry> @@ -57,7 +57,7 @@ by the options.</simpara> </term> <listitem> <simpara> - Read the contents of the given <emphasis>Entry</emphasis> from the <emphasis role="strong">Passwords</emphasis> section of the + Read the contents of the given <emphasis>Entry</emphasis> from the <emphasis role="strong">Folder</emphasis> section of the <emphasis>wallet</emphasis> and output it on the standard output. </simpara> </listitem> @@ -68,7 +68,7 @@ by the options.</simpara> </term> <listitem> <simpara> - Write secrets to the given <emphasis>Entry</emphasis> under the <emphasis role="strong">Passwords</emphasis> section of the given + Write secrets to the given <emphasis>Entry</emphasis> under the <emphasis role="strong">Folder</emphasis> section of the given <emphasis>wallet</emphasis>. The secrets are read from the standard input. <emphasis role="strong">IMPORTANT</emphasis> previous wallet entry value will be overwritten by this option, so be careful when using it! @@ -77,6 +77,16 @@ by the options.</simpara> </varlistentry> <varlistentry> <term> +<emphasis role="strong">-f,--folder</emphasis> <emphasis>Folder</emphasis> +</term> +<listitem> +<simpara> + Set the <emphasis>wallet</emphasis> folder to <emphasis>Folder</emphasis> value. By default <emphasis role="strong">Passwords</emphasis> is used. +</simpara> +</listitem> +</varlistentry> +<varlistentry> +<term> <emphasis role="strong">-v,--verbose</emphasis> </term> <listitem> @@ -127,7 +137,7 @@ by the options.</simpara> </term> <listitem> <simpara> - The <emphasis role="strong">Passwords</emphasis> section was not found inside the wallet <emphasis>wallet</emphasis>. Perhaps the + The <emphasis role="strong">Folder</emphasis> section was not found inside the wallet <emphasis>wallet</emphasis>. Perhaps the wallet file is corrupt? </simpara> </listitem> diff --git a/src/main.cpp b/src/main.cpp index 34beb73..b37c944 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,12 +47,14 @@ int main(int argc, char* argv[]) { QCommandLineOption listOption(QStringList() << "l" << "list-entries", i18n("list password entries")); QCommandLineOption readOption(QStringList() << "r" << "read-password", i18n("reads the secrets from the given <entry>"), i18n("Entry")); QCommandLineOption writeOption(QStringList() << "w" << "write-password", i18n("write secrets to the given <entry>. The values are read from the standard input. IMPORTANT: previous wallet entry value will be overwritten!"), i18n("Entry")); + QCommandLineOption folderOption(QStringList() << "f" << "folder", i18n("specify the folder in the wallet <folder>"), i18n("Folder")); cmdParser.addHelpOption(); cmdParser.addPositionalArgument(I18N_NOOP("wallet"), i18n("The wallet to query")); cmdParser.addOption(listOption); cmdParser.addOption(readOption); cmdParser.addOption(writeOption); + cmdParser.addOption(folderOption); cmdParser.addOption(verboseOption); cmdParser.process(app); @@ -89,6 +91,9 @@ int main(int argc, char* argv[]) { app.setEntryName(cmdParser.value(writeOption)); app.setMode(QueryDriver::Write); } + if (cmdParser.isSet(folderOption)) { + app.setEntryFolder(cmdParser.value(folderOption)); + } if (cmdParser.isSet(verboseOption)) { app.setVerbose(); } diff --git a/src/querydriver.cpp b/src/querydriver.cpp index e7282d4..1635a21 100644 --- a/src/querydriver.cpp +++ b/src/querydriver.cpp @@ -34,6 +34,7 @@ QueryDriver::QueryDriver(int &argc, char* argv[]) : QApplication(argc, argv) , theWallet(0) , verbose(false) + , entryFolder("Passwords") { QTimerEvent *timerEvent = new QTimerEvent(100); postEvent(this, timerEvent); @@ -78,10 +79,10 @@ void QueryDriver::walletOpened(bool success) { } else { switch (mode) { case List: - readPasswordEntries(); + readEntries(); break; case Read: - readPasswordValue(); + readValue(); break; case Write: writePasswordValue(); @@ -92,26 +93,49 @@ void QueryDriver::walletOpened(bool success) { } } -void QueryDriver::readPasswordEntries() { +void QueryDriver::readEntries() { theWallet = Wallet::openWallet(walletName, 0); auto fl = theWallet->folderList(); - if (fl.indexOf("Passwords") == -1) { - std::cout << i18n("'Passwords' folder not found").toStdString() << std::endl; - exit(3); + for (auto f: fl) { + std::cout << f.toStdString() << std::endl; + theWallet->setFolder(f); + auto el = theWallet->entryList(); + for (auto e: el) { + std::cout << "\t" << e.toStdString() << std::endl; + } } - theWallet->setFolder("Passwords"); - auto el = theWallet->entryList(); - for (auto e: el) { - if (theWallet->entryType(e) == Wallet::Password) - std::cout << e.toStdString() << std::endl; + quit(); +} + +void QueryDriver::readValue() { + if (verbose) qDebug() << "reading" << entryName << "from" << entryFolder << "from" << walletName; + theWallet->setFolder(entryFolder); + Wallet::EntryType kind = theWallet->entryType(entryName); + if (kind == Wallet::Password) { + readPasswordValue(); + } else if (kind == Wallet::Map) { + readMapValue(); + } else { + std::cout << i18n("Failed to read entry %1 value from the %2 wallet.", entryName, walletName).toStdString() << std::endl; + exit(4); } quit(); } +void QueryDriver::readMapValue() { + QMap<QString, QString> map; + int rc = theWallet->readMap(entryName, map); + if (rc != 0) { + std::cout << i18n("Failed to read entry %1 value from the %2 wallet", entryName, walletName).toStdString() << std::endl; + exit(4); + } + for (auto e : map.keys()) { + std::cout << e.toStdString() << ": " << map.value(e).toStdString() << std::endl; + } +} + void QueryDriver::readPasswordValue() { - if (verbose) qDebug() << "reading " << entryName << " from " << walletName; QString entryValue; - theWallet->setFolder("Passwords"); int rc = theWallet->readPassword(entryName, entryValue); if (rc != 0) { std::cout << i18n("Failed to read entry %1 value from the %2 wallet", entryName, walletName).toStdString() << std::endl; @@ -121,12 +145,18 @@ void QueryDriver::readPasswordValue() { for (auto e : el) { std::cout << e.toStdString() << std::endl; } - quit(); } void QueryDriver::writePasswordValue() { - if (verbose) qDebug() << "writing " << entryName << " to " << walletName; - theWallet->setFolder("Passwords"); + if (verbose) qDebug() << "writing" << entryName << "to" << entryFolder << "to" << walletName; + theWallet->setFolder(entryFolder); + + Wallet::EntryType kind = theWallet->entryType(entryName); + if (kind != Wallet::Password) { + std::cout << i18n("You can only write password values. Maps are not supported.").toStdString() << std::endl; + exit(4); + } + QString passwordContents; for (std::string line; std::getline(std::cin, line); ) { if (!passwordContents.isEmpty()) passwordContents += '\n'; diff --git a/src/querydriver.h b/src/querydriver.h index 83b4d0d..3f502b2 100644 --- a/src/querydriver.h +++ b/src/querydriver.h @@ -42,10 +42,13 @@ public: void setMode(Mode mode); void setVerbose() { verbose = true; } void setEntryName(const QString& entryName) { this->entryName = entryName; } + void setEntryFolder(const QString& entryFolder) { this->entryFolder = entryFolder; } private: virtual void timerEvent(QTimerEvent* event); - void readPasswordEntries(); + void readEntries(); + void readValue(); + void readMapValue(); void readPasswordValue(); void writePasswordValue(); @@ -58,5 +61,6 @@ public: Mode mode; bool verbose; QString entryName; + QString entryFolder; };
