/*************************************************************************** * Copyright (C) 2006 by Michael Kaufmann * * michael@enlighter.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 of the License, or * * (at your option) any later version. * * * * 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, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "authmanager.h" #include "kovpnconfig.h" #include #include #include #include using namespace std; authManager::authManager() { wallet = NULL; } authManager::~authManager() {} bool authManager::authRequest( const QString &ressource, const QString &type, QString &username, QString &password, bool failure, const QString &message, bool saveAccount ) { /* parts this code is taken from http://www.staikos.net/~staikos/presentations/August2003/kwallet/html/slide_19.html */ QByteArray temp_username; bool cancel; debug( "authRequest", QString( "Ressource : %1" ).arg( ressource ) ); debug( "authRequest", QString( "Failure : %1" ).arg( failure ) ); debug( "authRequest", QString( "saveAccount : %1" ).arg( saveAccount ) ); /* First look if I should use kwallet or if there was an failure (= stored username/password is wrong) */ if ( saveAccount && ! failure ) { /* Try to open the wallet */ wallet = openWallet(); if ( wallet ) { if ( wallet->setFolder( "kovpn" ) ) { /* If both keys are in the wallet then use them, otherwise ask the GUI */ if ( wallet->hasEntry( type + "_" + ressource ) ) { QMap authData; wallet->readMap( type + "_" + ressource, authData ); /* Check if exactly the key I need is stored in the wallet, because it may happen that there is a username and password * and a private key passphrase and a management interface password is stored in it. The existance of the entry "ressource" * is not enough in this case. */ if ( authData.find( "username" ) != authData.end() && authData.find( "password" ) != authData.end() ) { username = authData[ "username" ]; password = authData[ "password" ]; return true; } } } else { emit error ( i18n( "Cannot access my kwallet folder, though it seems to exist." ) ); /* Ask user for password */ } } else { /* Ask user for password */ } } if ( failure ) { /* Notify the GUI of an error happened */ emit error( i18n( "The supplied username and/or password is wrong." ) ); } /* Ask user for password */ if ( authRequestGUI( cancel, username, password, ressource, message ) ) { /* Store username and password in the wallet */ if ( saveAccount ) { ( void ) storeInWallet( ressource, type, password, username ); } return true; } else { return false; } } bool authManager::authRequest( const QString & ressource, const QString &type, QString & passphrase, bool failure, const QString & message, bool savePassphrase ) { /* parts this code is taken from http://www.staikos.net/~staikos/presentations/August2003/kwallet/html/slide_19.html */ QByteArray temp_username; bool cancel; debug( "authRequest", QString( "Ressource : %1" ).arg( ressource ) ); debug( "authRequest", QString( "Failure : %1" ).arg( failure ) ); debug( "authRequest", QString( "savePassphrase : %1" ).arg( savePassphrase ) ); /* First look if I should use kwallet or if there was an failure (= stored username/password is wrong) */ if ( savePassphrase && ! failure ) { /* Try to open the wallet */ wallet = openWallet(); if ( wallet ) { if ( wallet->setFolder( "kovpn" ) ) { /* If both keys are in the wallet then use them, otherwise ask the GUI */ if ( wallet->hasEntry( type + "_" + ressource ) ) { QMap authData; wallet->readMap( type + "_" + ressource, authData ); /* Check if exactly the key I need is stored in the wallet, because it may happen that there is a username and password * and a private key passphrase and a management interface password is stored in it. The existance of the entry "ressource" * is not enough in this case. */ if ( authData.find( "passphrase" ) != authData.end() ) { passphrase = authData[ "passphrase" ]; return true; } } } else { emit error ( i18n( "Cannot access my kwallet folder, though it seems to exist." ) ); /* Ask user for password */ } } else { /* Ask user for password */ } } if ( failure ) { /* Notify the GUI of an error happened */ emit error( i18n( "The supplied private key passphrase is wrong." ) ); } /* Ask user for password */ if ( passRequestGUI( cancel, passphrase, ressource, message ) ) { /* Store private key passphrase in the wallet */ if ( savePassphrase ) { ( void ) storeInWallet( ressource, type, passphrase ); } return true; } else { return false; } } void authManager::walletClosed() {} bool authManager::authRequestGUI( bool & cancel, QString & username, QString & password, const QString & ressource, const QString & message ) { bool failure = true; emit authRequestGUI( cancel, failure, ressource, password, username, message ); if ( failure ) { username = QString::null; password = QString::null; return false; } return true; } bool authManager::passRequestGUI( bool & cancel, QString & passphrase, const QString & ressource, const QString & message ) { bool failure = true; emit passRequestGUI( cancel, failure, ressource, passphrase, message ); if ( failure ) { passphrase = QString::null; return false; } return true; } /** * This method tries to open a wallet and the right folder for kovpn. If the folder doesn't exist * it tries to create it. If the folder exists or could be created it returns a pointer to a wallet. * otherwise it returns NULL. */ KWallet::Wallet * authManager::openWallet() { /* parts this code is taken from http://www.staikos.net/~staikos/presentations/August2003/kwallet/html/slide_19.html */ /* Look if kwallet is enabled */ if ( KWallet::Wallet::isEnabled() ) { debug( "openWallet", "KWallet is globally enabled. All Right!" ); if ( ! wallet ) { debug( "openWallet", "Kwallet is not yet open" ); /* Open the wallet if it's not openened yet */ wallet = KWallet::Wallet::openWallet( KWallet::Wallet::NetworkWallet() ); if ( wallet ) { debug( "openWallet", "KWallet is open" ); /****************************************************/ // wallet->removeFolder( "kovpn" ); // connect( wallet, SIGNAL( walletClosed() ), this, SLOT( walledClosed() ) ); /* look for my folder and create it if it doesn't exist. */ if ( ! wallet->hasFolder( "kovpn" ) ) { debug( "openWallet", "kovpn folder does not exist in KWallet" ); if ( ! wallet->createFolder( "kovpn" ) ) { debug( "openWallet", "Could not create kovpn folder in KWallet" ); delete wallet; wallet = NULL; emit error ( i18n( "Some strange error happend using kwallet. I cannot create a folder." ) ); } } } } } else { wallet = NULL; emit error ( i18n( "KDE Wallet is disabled. Please enable it first to use it." ) ); /* Ask user for password */ } return wallet; } bool authManager::storeInWallet( const QString &ressource, const QString &type, const QString &password, const QString &username ) { /* parts this code is taken from http://www.staikos.net/~staikos/presentations/August2003/kwallet/html/slide_19.html */ /* Try to open the wallet */ wallet = openWallet(); if ( wallet ) { if ( wallet->setFolder( "kovpn" ) ) { QMap authData; if ( username != QString::null ) { /* Sometimes there is no need for a username, e.g. a passphrase for a private key. */ authData.insert( "username", username ); authData.insert( "password", password ); } else { authData.insert( "passphrase", password ); } wallet->writeMap( type + "_" + ressource, authData ); // wallet->writePassword( ressource, password ); } else { emit error ( i18n( "Cannot access my kwallet folder, though it seems to exist." ) ); return false; } } else { emit error ( i18n( "Cannot open my wallet. Username and password are not stored." ) ); return false; } return true; } authManager * authManager::manager() { return self(); } authManager * authManager::self() { static authManager * m_manager = new authManager(); return m_manager; } void authManager::debug( const QString & method, const QString & message ) { #ifdef DEBUG QString myMethod ( method ); static unsigned int maxLen = 0; maxLen = QMAX( myMethod.length(), maxLen ); cout << "authManager::" << myMethod.leftJustify( maxLen ) << " => " << message << endl; #endif } bool authManager::newPassword( const QString & ressource, const QString & type, QString & password, const QString & message, bool savePassword ) { /* parts this code is taken from http://www.staikos.net/~staikos/presentations/August2003/kwallet/html/slide_19.html */ QByteArray temp_username; bool cancel; bool failure; debug( "newPassword", QString( "Ressource : %1" ).arg( ressource ) ); debug( "newPassword", QString( "savePassword : %1" ).arg( savePassword ) ); /* Ask user for password */ if ( newPasswordGUI( cancel, password, ressource, message ) ) { /* Store private key passphrase in the wallet */ if ( savePassword ) { ( void ) storeInWallet( ressource, type, password ); } return true; } else { return false; } } bool authManager::newPasswordGUI( bool & cancel, QString & password, const QString & ressource, const QString & message ) { bool failure = true; emit newPasswordGUI( cancel, failure, ressource, password, message ); if ( failure ) { password = QString::null; return false; } return true; } #include "authmanager.moc"