added new individual config saving system

global signature file format will be converted


git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3514 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
chrisparker126 2010-09-18 19:09:11 +00:00
parent 831c73dd9d
commit 52e0d2a095
9 changed files with 830 additions and 24 deletions

View file

@ -45,6 +45,11 @@ p3ConfigMgr::p3ConfigMgr(std::string dir, std::string fname, std::string signame
:basedir(dir), metafname(fname), metasigfname(signame),
mConfigSaveActive(true)
{
oldConfigType = checkForGlobalSigConfig();
// configuration to load correct global types
pqiConfig::globalConfigType = oldConfigType;
}
void p3ConfigMgr::tick()
@ -90,10 +95,59 @@ void p3ConfigMgr::saveConfiguration()
if(!RsDiscSpace::checkForDiscSpace(RS_CONFIG_DIRECTORY))
return ;
saveConfig();
}
void p3ConfigMgr::saveConfig()
{
bool ok= true;
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
std::map<uint32_t, pqiConfig *>::iterator it;
for(it = configs.begin(); it != configs.end(); it++)
{
if (it->second->HasConfigChanged(1))
{
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::globalSaveConfig() Saving Element: ";
std::cerr << it->first;
std::cerr << std::endl;
#endif
ok &= it->second->saveConfiguration();
}
/* save metaconfig */
}
if(ok && oldConfigType)
removeOldConfigType();
return;
}
void p3ConfigMgr::removeOldConfigType()
{
std::string fName = basedir + "/" + metafname;
std::string sigfName = basedir + "/" + metasigfname;
remove(fName.c_str());
remove(sigfName.c_str());
//now set globalconfig type to false so mgr saves
oldConfigType = false;
pqiConfig::globalConfigType = oldConfigType;
}
void p3ConfigMgr::globalSaveConfig()
{
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::saveConfiguration()";
std::cerr << "p3ConfigMgr::globalSaveConfig()";
std::cerr << std::endl;
#endif
@ -105,7 +159,7 @@ void p3ConfigMgr::saveConfiguration()
if (it->second->HasConfigChanged(1))
{
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::saveConfiguration() Saving Element: ";
std::cerr << "p3ConfigMgr::globalSaveConfig() Saving Element: ";
std::cerr << it->first;
std::cerr << std::endl;
#endif
@ -114,7 +168,7 @@ void p3ConfigMgr::saveConfiguration()
/* save metaconfig */
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::saveConfiguration() Element: ";
std::cerr << "p3ConfigMgr::globalSaveConfig() Element: ";
std::cerr << it->first << " Hash: " << it->second->Hash();
std::cerr << std::endl;
#endif
@ -135,7 +189,7 @@ void p3ConfigMgr::saveConfiguration()
}
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::saveConfiguration() Complete MetaConfigItem: ";
std::cerr << "p3ConfigMgr::globalSaveConfig() Complete MetaConfigItem: ";
std::cerr << std::endl;
item->print(std::cerr, 20);
@ -174,7 +228,7 @@ void p3ConfigMgr::saveConfiguration()
signature.length(), BIN_FLAGS_READABLE);
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::saveConfiguration() MetaFile Signature:";
std::cerr << "p3ConfigMgr::globalSaveConfig() MetaFile Signature:";
std::cerr << std::endl;
std::cerr << signature;
std::cerr << std::endl;
@ -184,6 +238,7 @@ void p3ConfigMgr::saveConfiguration()
backedUpFileSave(fname, fname_backup, sign_fname, sign_fname_backup, configbio, signbio);
delete signbio;
}
bool p3ConfigMgr::backedUpFileSave(const std::string& fname, const std::string& fname_backup, const std::string& sign_fname,
@ -292,7 +347,39 @@ bool p3ConfigMgr::backedUpFileSave(const std::string& fname, const std::string&
return true;
}
void p3ConfigMgr::loadConfiguration()
void p3ConfigMgr::loadConfiguration()
{
if(oldConfigType)
globalLoadConfig();
else
loadConfig();
return;
}
void p3ConfigMgr::loadConfig()
{
std::map<uint32_t, pqiConfig *>::iterator cit;
std::string dummyHash = "dummyHash";
for (cit = configs.begin(); cit != configs.end(); cit++)
{
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::loadConfig() Element: ";
std::cerr << cit->first <<"Dummy Hash: " << dummyHash;
std::cerr << std::endl;
#endif
cit->second->loadConfiguration(dummyHash);
/* force config to NOT CHANGED */
cit->second->HasConfigChanged(0);
cit->second->HasConfigChanged(1);
}
return;
}
void p3ConfigMgr::globalLoadConfig()
{
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
@ -395,9 +482,11 @@ void p3ConfigMgr::loadConfiguration()
std::cerr << std::endl;
#endif
(cit->second)->loadConfiguration(hashin);
/* force config to NOT CHANGED */
cit->second->HasConfigChanged(0);
cit->second->HasConfigChanged(1);
/* force config to CHANGED to force saving into new non-global sig format */
cit->second->IndicateConfigChanged();
// cit->second->HasConfigChanged(0);
// cit->second->HasConfigChanged(1);
}
}
@ -532,6 +621,33 @@ void p3ConfigMgr::completeConfiguration()
mConfigSaveActive = false;
}
bool p3ConfigMgr::checkForGlobalSigConfig()
{
bool oldTypeExists;
FILE *metaFile = NULL, *metaSig = NULL;
std::string fName = basedir + "/" + metafname;
std::string sigName = basedir + "/" + metasigfname;
metaFile = fopen(fName.c_str(), "r");
metaSig = fopen(sigName.c_str(), "r");
// check if files exist
if((metaFile != NULL) && (metaSig != NULL))
{
oldTypeExists = true;
fclose(metaFile);
fclose(metaSig);
}
else
oldTypeExists = false;
return oldTypeExists;
}
p3Config::p3Config(uint32_t t)
:pqiConfig(t)
{
@ -539,7 +655,121 @@ p3Config::p3Config(uint32_t t)
}
bool p3Config::loadConfiguration(std::string &loadHash)
bool p3Config::loadConfiguration(std::string &loadHash)
{
if(globalConfigType)
return loadGlobalConfig(loadHash);
else
return loadConfig();
}
bool p3Config::loadConfig()
{
#ifdef CONFIG_DEBUG
std::cerr << "p3Config::loadConfig() loading Configuration\n File: " << Filename() << std::endl;
#endif
bool pass = true;
std::string cfgFname = Filename();
std::string cfgFnameBackup = cfgFname + ".tmp";
std::string signFname = Filename() +".sgn";
std::string signFnameBackup = signFname + ".tmp";
std::list<RsItem *> load;
std::list<RsItem *>::iterator it;
// try 1st attempt
if(!loadAttempt(cfgFname, signFname, load))
{
#ifdef CONFIG_DEBUG
std::cerr << "p3Config::loadConfig() Failed to Load" << std::endl;
#endif
/* bad load */
for(it = load.begin(); it != load.end(); it++)
{
delete (*it);
}
pass = false;
}
// try 2nd attempt with backup files if first failed
if(!pass)
{
if(!loadAttempt(cfgFnameBackup, signFnameBackup, load))
{
#ifdef CONFIG_DEBUG
std::cerr << "p3Config::loadConfig() Failed on 2nd Pass" << std::endl;
#endif
/* bad load */
for(it = load.begin(); it != load.end(); it++)
{
delete (*it);
}
pass = false;
}
}
if(pass)
loadList(load);
else
return false;
return pass;
}
bool p3Config::loadAttempt(const std::string& cfgFname,const std::string& signFname, std::list<RsItem *>& load)
{
#ifdef CONFIG_DEBUG
std::cerr << "p3Config::loadAttempt() \nFilename: " << cfgFname << std::endl;
#endif
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_READABLE;
uint32_t stream_flags = BIN_FLAGS_READABLE;
BinEncryptedFileInterface *bio = new BinEncryptedFileInterface(cfgFname.c_str(), bioflags);
pqiSSLstore stream(setupSerialiser(), "CONFIG", bio, stream_flags);
if(!stream.getEncryptedItems(load))
{
#ifdef CONFIG_DEBUG
std::cerr << "p3Config::loadAttempt() Error occurred trying to load Item" << std::endl;
#endif
return false;
}
/* set hash */
setHash(bio->gethash());
BinMemInterface *signbio = new BinMemInterface(1000, BIN_FLAGS_READABLE);
if(!signbio->readfromfile(signFname.c_str()))
return false;
std::string signatureStored((char *) signbio->memptr(), signbio->memsize());
std::string signatureRead;
std::string strHash(Hash());
AuthSSL::getAuthSSL()->SignData(strHash.c_str(), strHash.length(), signatureRead);
if(signatureRead != signatureStored)
return false;
delete signbio;
return true;
}
bool p3Config::loadGlobalConfig(std::string &loadHash)
{
bool pass = false;
std::string cfg_fname = Filename();
@ -637,7 +867,94 @@ bool p3Config::getHashAttempt(const std::string& loadHash, std::string& hashstr,
}
bool p3Config::saveConfiguration()
bool p3Config::saveConfiguration()
{
return saveConfig();
}
bool p3Config::saveConfig()
{
bool cleanup = true;
std::list<RsItem *> toSave = saveList(cleanup);
// temporarily append new to files as these will replace current configuration
std::string newCfgFname = Filename() + "_new";
std::string newSignFname = Filename() + ".sgn" + "_new";
std::string tmpCfgFname = Filename() + ".tmp";
std::string tmpSignFname = Filename() + ".sgn" + ".tmp";
std::string cfgFname = Filename();
std::string signFname = Filename() + ".sgn";
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_WRITEABLE;
uint32_t stream_flags = BIN_FLAGS_WRITEABLE;
bool written = true;
if (!cleanup)
stream_flags |= BIN_FLAGS_NO_DELETE;
BinEncryptedFileInterface *cfg_bio = new BinEncryptedFileInterface(newCfgFname.c_str(), bioflags);
pqiSSLstore *stream = new pqiSSLstore(setupSerialiser(), "CONFIG", cfg_bio, stream_flags);
written = written && stream->encryptedSendItems(toSave);
/* store the hash */
setHash(cfg_bio->gethash());
// bio is taken care of in stream's destructor, also forces file to close
delete stream;
/* sign data */
std::string signature;
std::string strHash(Hash());
AuthSSL::getAuthSSL()->SignData(strHash.c_str(),strHash.length(), signature);
/* write signature to configuration */
BinMemInterface *signbio = new BinMemInterface(signature.c_str(),
signature.length(), BIN_FLAGS_READABLE);
signbio->writetofile(newSignFname.c_str());
delete signbio;
// now rewrite current files to temp files
// rename back-up to current file
if(!RsDirUtil::renameFile(cfgFname, tmpCfgFname) || !RsDirUtil::renameFile(signFname, tmpSignFname)){
#ifdef CONFIG_DEBUG
std::cerr << "p3Config::backedUpFileSave() Failed to rename backup meta files: " << std::endl
<< cfgFname << " to " << tmpCfgFname << std::endl
<< signFname << " to " << tmpSignFname << std::endl;
#endif
written = false;
}
// now rewrite current files to temp files
// rename back-up to current file
if(!RsDirUtil::renameFile(newCfgFname, cfgFname) || !RsDirUtil::renameFile(newSignFname, signFname)){
#ifdef CONFIG_DEBUG
std::cerr << "p3Config::() Failed to rename meta files: " << std::endl
<< newCfgFname << " to " << cfgFname << std::endl
<< newSignFname << " to " << signFname << std::endl;
#endif
written = false;
}
saveDone(); // callback to inherited class to unlock any Mutexes protecting saveList() data
return written;
}
bool p3Config::saveGlobalConfig()
{
bool cleanup = true;
@ -945,6 +1262,8 @@ bool p3GeneralConfig::loadList(std::list<RsItem *> load)
* only the Indication and hash really need it
*/
bool pqiConfig::globalConfigType = false;
pqiConfig::pqiConfig(uint32_t t)
:ConfInd(2), type(t)
{