File indexing completed on 2024-05-12 17:15:58

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 <QPointer>
0026 #include <QTextStream>
0027 #include <QApplication>
0028 #include <QCommandLineParser>
0029 #include <QCommandLineOption>
0030 
0031 int main(int argc, char **argv)
0032 {
0033     QApplication app(argc, argv);
0034     KLocalizedString::setApplicationDomain("kdesvn");
0035     KAboutData aboutData(QStringLiteral("kdesvnaskpass"), i18n("kdesvnaskpass"), QStringLiteral("0.2"),
0036                          i18n("ssh-askpass for kdesvn"),
0037                          KAboutLicense::LicenseKey::LGPL,
0038                          i18n("Copyright (c) 2005-2009 Rajko Albrecht"));
0039     QCommandLineParser parser;
0040     KAboutData::setApplicationData(aboutData);
0041     parser.addVersionOption();
0042     parser.addHelpOption();
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) ||
0059                 prompt.contains(QLatin1String("Permission denied"), Qt::CaseInsensitive)) {
0060             error = true;
0061         }
0062         kfile = prompt.section(QLatin1Char(' '), -2).remove(QLatin1Char(':')).simplified();
0063     }
0064     QString pw;
0065     QString wfolder = aboutData.productName();
0066 
0067     QScopedPointer<KWallet::Wallet> wallet(KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), 0));
0068     if (!error && wallet && wallet->hasFolder(wfolder)) {
0069         wallet->setFolder(wfolder);
0070         wallet->readPassword(kfile, pw);
0071     }
0072 
0073     if (pw.isEmpty()) {
0074         QPointer<KPasswordDialog> dlg(new KPasswordDialog(nullptr, (wallet ? KPasswordDialog::ShowKeepPassword : KPasswordDialog::NoFlags)));
0075         dlg->setPrompt(prompt);
0076         dlg->setWindowTitle(i18nc("@title:window", "Password"));
0077         if (dlg->exec() != KPasswordDialog::Accepted) {
0078             delete dlg;
0079             return 1;
0080         }
0081         pw = dlg->password();
0082         if (wallet && dlg->keepPassword()) {
0083             if (!wallet->hasFolder(wfolder)) {
0084                 wallet->createFolder(wfolder);
0085             }
0086             wallet->setFolder(wfolder);
0087             wallet->writePassword(kfile, pw);
0088         }
0089         delete dlg;
0090     }
0091 
0092     QTextStream out(stdout);
0093     out << pw;
0094     /* cleanup memory */
0095     pw.replace(0, pw.length(), QLatin1Char('0'));
0096     return 0;
0097 }
0098