File indexing completed on 2024-05-05 05:44:47

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2009 by Rajko Albrecht                             *
0003  *   ral@alwins-world.de                                                   *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 #include <KAboutData>
0021 #include <KLocalizedString>
0022 #include <KPasswordDialog>
0023 #include <KWallet>
0024 
0025 #include <QApplication>
0026 #include <QCommandLineOption>
0027 #include <QCommandLineParser>
0028 #include <QPointer>
0029 #include <QTextStream>
0030 
0031 int main(int argc, char **argv)
0032 {
0033     QApplication app(argc, argv);
0034     KLocalizedString::setApplicationDomain("kdesvn");
0035     KAboutData aboutData(QStringLiteral("kdesvnaskpass"),
0036                          i18n("kdesvnaskpass"),
0037                          QStringLiteral("0.2"),
0038                          i18n("ssh-askpass for kdesvn"),
0039                          KAboutLicense::LicenseKey::LGPL,
0040                          i18n("Copyright (c) 2005-2009 Rajko Albrecht"));
0041     QCommandLineParser parser;
0042     KAboutData::setApplicationData(aboutData);
0043     parser.addPositionalArgument(QStringLiteral("[prompt]"), i18n("Prompt"));
0044     aboutData.setupCommandLine(&parser);
0045     parser.process(app);
0046     aboutData.processCommandLine(&parser);
0047     // no need for session management
0048     // app.disableSessionManagement();
0049 
0050     QString prompt;
0051     QString kfile;
0052     bool error = false;
0053 
0054     if (parser.positionalArguments().isEmpty()) {
0055         prompt = i18n("Please enter your password below.");
0056     } else {
0057         prompt = parser.positionalArguments().at(0);
0058         if (prompt.contains(QLatin1String("Bad passphrase"), Qt::CaseInsensitive) || prompt.contains(QLatin1String("Permission denied"), Qt::CaseInsensitive)) {
0059             error = true;
0060         }
0061         kfile = prompt.section(QLatin1Char(' '), -2).remove(QLatin1Char(':')).simplified();
0062     }
0063     QString pw;
0064     QString wfolder = aboutData.productName();
0065 
0066     QScopedPointer<KWallet::Wallet> wallet(KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), 0));
0067     if (!error && wallet && wallet->hasFolder(wfolder)) {
0068         wallet->setFolder(wfolder);
0069         wallet->readPassword(kfile, pw);
0070     }
0071 
0072     if (pw.isEmpty()) {
0073         QPointer<KPasswordDialog> dlg(new KPasswordDialog(nullptr, (wallet ? KPasswordDialog::ShowKeepPassword : KPasswordDialog::NoFlags)));
0074         dlg->setPrompt(prompt);
0075         dlg->setWindowTitle(i18nc("@title:window", "Password"));
0076         if (dlg->exec() != KPasswordDialog::Accepted) {
0077             delete dlg;
0078             return 1;
0079         }
0080         pw = dlg->password();
0081         if (wallet && dlg->keepPassword()) {
0082             if (!wallet->hasFolder(wfolder)) {
0083                 wallet->createFolder(wfolder);
0084             }
0085             wallet->setFolder(wfolder);
0086             wallet->writePassword(kfile, pw);
0087         }
0088         delete dlg;
0089     }
0090 
0091     QTextStream out(stdout);
0092     out << pw;
0093     /* cleanup memory */
0094     pw.replace(0, pw.length(), QLatin1Char('0'));
0095     return 0;
0096 }