- moved lock handle functions to rsdir.h/cc

- created a scope guard to manage file lock handles
- added lock gards to PGP keyring read/writes.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5216 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2012-06-12 20:31:13 +00:00
parent 1885fb66c4
commit f30a3f1b16
7 changed files with 185 additions and 98 deletions

View file

@ -16,6 +16,7 @@ extern "C" {
}
#include "pgphandler.h"
#include "retroshare/rsiface.h" // For rsicontrol.
#include "util/rsdir.h" // For rsicontrol.
PassphraseCallback PGPHandler::_passphrase_callback = NULL ;
@ -67,9 +68,14 @@ void PGPHandler::setPassphraseCallback(PassphraseCallback cb)
_passphrase_callback = cb ;
}
PGPHandler::PGPHandler(const std::string& pubring, const std::string& secring)
: pgphandlerMtx(std::string("PGPHandler")), _pubring_path(pubring),_secring_path(secring)
PGPHandler::PGPHandler(const std::string& pubring, const std::string& secring,const std::string& pgp_lock_filename)
: pgphandlerMtx(std::string("PGPHandler")), _pubring_path(pubring),_secring_path(secring),_pgp_lock_filename(pgp_lock_filename)
{
_pubring_changed = false ;
_secring_changed = false ;
RsStackFileLock flck(_pgp_lock_filename) ; // lock access to PGP directory.
if(_passphrase_callback == NULL)
{
std::cerr << "WARNING: before created a PGPHandler, you need to init the passphrase callback using PGPHandler::setPassphraseCallback()" << std::endl;
@ -137,7 +143,6 @@ PGPHandler::PGPHandler(const std::string& pubring, const std::string& secring)
}
std::cerr << "Secring read successfully." << std::endl;
}
void PGPHandler::initCertificateInfo(PGPCertificateInfo& cert,const ops_keydata_t *keydata,uint32_t index)
@ -338,6 +343,9 @@ bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::stri
// validateAndUpdateSignatures(_public_keyring_map[ pgpId.toStdString() ],getPublicKey(pgpId)) ;
_pubring_changed = true ;
_secring_changed = true ;
return true ;
}
@ -440,12 +448,25 @@ bool PGPHandler::LoadCertificateFromString(const std::string& pgp_cert,PGPIdType
ops_keyring_free(tmp_keyring) ;
free(tmp_keyring) ;
_pubring_changed = true ;
return true ;
}
bool PGPHandler::writePublicKeyring(const std::string& outfilename) const
bool PGPHandler::writePublicKeyring()
{
return ops_write_keyring_to_file(_pubring,ops_false,outfilename.c_str()) ;
RsStackFileLock flck(_pgp_lock_filename) ; // locks access to pgp directory
_pubring_changed = false ;
return ops_write_keyring_to_file(_pubring,ops_false,_pubring_path.c_str()) ;
}
bool PGPHandler::writeSecretKeyring()
{
RsStackFileLock flck(_pgp_lock_filename) ; // locks access to pgp directory
_secring_changed = false ;
return ops_write_keyring_to_file(_secring,ops_false,_secring_path.c_str()) ;
}
bool PGPHandler::encryptTextToFile(const PGPIdType& key_id,const std::string& text,const std::string& outfile)

View file

@ -52,7 +52,7 @@ class PGPCertificateInfo
class PGPHandler
{
public:
PGPHandler(const std::string& path_to_public_keyring, const std::string& path_to_secret_keyring) ;
PGPHandler(const std::string& path_to_public_keyring, const std::string& path_to_secret_keyring, const std::string& pgp_lock_file) ;
virtual ~PGPHandler() ;
@ -79,10 +79,11 @@ class PGPHandler
void setAcceptConnexion(const PGPIdType&,bool) ;
// Write keyring
bool writePublicKeyring(const std::string& filename) const ;
bool publicKeyringChanged() const { return _pubring_changed ; }
bool secretKeyringChanged() const { return _secring_changed ; }
// Debug stuff.
virtual bool printKeys() const ;
bool writeSecretKeyring() ;
bool writePublicKeyring() ;
const PGPCertificateInfo *getCertificateInfo(const PGPIdType& id) const ;
@ -92,6 +93,10 @@ class PGPHandler
static void setPassphraseCallback(PassphraseCallback cb) ;
static PassphraseCallback passphraseCallback() { return _passphrase_callback ; }
// Debug stuff.
virtual bool printKeys() const ;
private:
void initCertificateInfo(PGPCertificateInfo& cert,const ops_keydata_t *keydata,uint32_t i) ;
void validateAndUpdateSignatures(PGPCertificateInfo& cert,const ops_keydata_t *keydata) ;
@ -111,6 +116,10 @@ class PGPHandler
const std::string _pubring_path ;
const std::string _secring_path ;
const std::string _pgp_lock_filename ;
bool _pubring_changed ;
bool _secring_changed ;
// Helper functions.
//