#define _GNU_SOURCE
#endif
-#include <pwd.h>
-#include <grp.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <sys/time.h>
-#include <fcntl.h>
-#include <dirent.h>
-
-#include <cstdio>
-#include <ctime>
-#include <cerrno>
-#include <cstring>
-#include <sstream>
-#include <algorithm>
+#include "file_store.h"
#include "stg/common.h"
#include "stg/user_ips.h"
#include "stg/blowfish.h"
#include "stg/logger.h"
#include "stg/locker.h"
-#include "stg/plugin_creator.h"
-#include "file_store.h"
+#include "stg/admin_conf.h"
+#include "stg/tariff.h"
+#include "stg/tariff_conf.h"
+#include "stg/service_conf.h"
+
+#include <sstream>
+#include <algorithm>
+#include <cstdio>
+#include <ctime>
+#include <cerrno>
+#include <cstring>
+
+#include <pwd.h>
+#include <grp.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <fcntl.h>
+#include <dirent.h>
#define DELETED_USERS_DIR "deleted_users"
//-----------------------------------------------------------------------------
namespace
{
-PLUGIN_CREATOR<FILES_STORE> fsc;
+
+bool CheckAndCreate(const std::string & dir, mode_t mode)
+{
+if (access(dir.c_str(), F_OK) == 0)
+ return true;
+if (mkdir(dir.c_str(), mode) == 0)
+ return true;
+return false;
}
-extern "C" STORE * GetStore();
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-STORE * GetStore()
+}
+
+extern "C" STG::Store* GetStore()
{
-return fsc.GetPlugin();
+ static FILES_STORE plugin;
+ return &plugin;
}
//-----------------------------------------------------------------------------
FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
{
}
//-----------------------------------------------------------------------------
-int FILES_STORE_SETTINGS::ParseOwner(const std::vector<PARAM_VALUE> & moduleParams, const std::string & owner, uid_t * uid)
+int FILES_STORE_SETTINGS::ParseOwner(const std::vector<STG::ParamValue> & moduleParams, const std::string & owner, uid_t * uid)
{
-PARAM_VALUE pv;
+STG::ParamValue pv;
pv.param = owner;
-std::vector<PARAM_VALUE>::const_iterator pvi;
+std::vector<STG::ParamValue>::const_iterator pvi;
pvi = find(moduleParams.begin(), moduleParams.end(), pv);
if (pvi == moduleParams.end() || pvi->value.empty())
{
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE_SETTINGS::ParseGroup(const std::vector<PARAM_VALUE> & moduleParams, const std::string & group, gid_t * gid)
+int FILES_STORE_SETTINGS::ParseGroup(const std::vector<STG::ParamValue> & moduleParams, const std::string & group, gid_t * gid)
{
-PARAM_VALUE pv;
+STG::ParamValue pv;
pv.param = group;
-std::vector<PARAM_VALUE>::const_iterator pvi;
+std::vector<STG::ParamValue>::const_iterator pvi;
pvi = find(moduleParams.begin(), moduleParams.end(), pv);
if (pvi == moduleParams.end() || pvi->value.empty())
{
return -1;
}
//-----------------------------------------------------------------------------
-int FILES_STORE_SETTINGS::ParseMode(const std::vector<PARAM_VALUE> & moduleParams, const std::string & modeStr, mode_t * mode)
+int FILES_STORE_SETTINGS::ParseMode(const std::vector<STG::ParamValue> & moduleParams, const std::string & modeStr, mode_t * mode)
{
-PARAM_VALUE pv;
+STG::ParamValue pv;
pv.param = modeStr;
-std::vector<PARAM_VALUE>::const_iterator pvi;
+std::vector<STG::ParamValue>::const_iterator pvi;
pvi = find(moduleParams.begin(), moduleParams.end(), pv);
if (pvi == moduleParams.end() || pvi->value.empty())
{
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
+int FILES_STORE_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
{
if (ParseOwner(s.moduleParams, "StatOwner", &statUID) < 0)
return -1;
if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0)
return -1;
-std::vector<PARAM_VALUE>::const_iterator pvi;
-PARAM_VALUE pv;
+std::vector<STG::ParamValue>::const_iterator pvi;
+STG::ParamValue pv;
pv.param = "RemoveBak";
pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
if (pvi == s.moduleParams.end() || pvi->value.empty())
workDir.resize(workDir.size() - 1);
}
usersDir = workDir + "/users/";
+if (!CheckAndCreate(usersDir, GetConfModeDir()))
+ {
+ errorStr = usersDir + " doesn't exist. Failed to create.";
+ printfd(__FILE__, "%s\n", errorStr.c_str());
+ return -1;
+ }
tariffsDir = workDir + "/tariffs/";
+if (!CheckAndCreate(tariffsDir, GetConfModeDir()))
+ {
+ errorStr = tariffsDir + " doesn't exist. Failed to create.";
+ printfd(__FILE__, "%s\n", errorStr.c_str());
+ return -1;
+ }
adminsDir = workDir + "/admins/";
+if (!CheckAndCreate(adminsDir, GetConfModeDir()))
+ {
+ errorStr = adminsDir + " doesn't exist. Failed to create.";
+ printfd(__FILE__, "%s\n", errorStr.c_str());
+ return -1;
+ }
+servicesDir = workDir + "/services/";
+if (!CheckAndCreate(servicesDir, GetConfModeDir()))
+ {
+ errorStr = servicesDir + " doesn't exist. Failed to create.";
+ printfd(__FILE__, "%s\n", errorStr.c_str());
+ return -1;
+ }
return 0;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
FILES_STORE::FILES_STORE()
- : errorStr(),
- version("file_store v.1.04"),
- storeSettings(),
- settings(),
- mutex(),
- logger(GetPluginLogger(GetStgLogger(), "store_files"))
+ : version("file_store v.1.04"),
+ logger(STG::PluginLogger::get("store_files"))
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
tariffList->swap(files);
+return 0;
+}
+//-----------------------------------------------------------------------------
+int FILES_STORE::GetServicesList(std::vector<std::string> * list) const
+{
+std::vector<std::string> files;
+
+if (GetFileList(&files, storeSettings.GetServicesDir(), S_IFREG, ".serv"))
+ {
+ STG_LOCKER lock(&mutex);
+ errorStr = "Failed to open '" + storeSettings.GetServicesDir() + "': " + std::string(strerror(errno));
+ return -1;
+ }
+
+STG_LOCKER lock(&mutex);
+
+list->swap(files);
+
return 0;
}
//-----------------------------------------------------------------------------
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const
+int FILES_STORE::RestoreUserConf(STG::UserConf * conf, const std::string & login) const
{
std::string fileName;
fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login, const std::string & fileName) const
+int FILES_STORE::RestoreUserConf(STG::UserConf * conf, const std::string & login, const std::string & fileName) const
{
CONFIGFILE cf(fileName);
int e = cf.Error();
std::string ipStr;
cf.ReadString("IP", &ipStr, "?");
-USER_IPS ips;
try
{
- ips = StrToIPS(ipStr);
+ conf->ips = STG::UserIPs::parse(ipStr);
}
catch (const std::string & s)
{
printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
return -1;
}
-conf->ips = ips;
if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
{
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const
+int FILES_STORE::RestoreUserStat(STG::UserStat * stat, const std::string & login) const
{
std::string fileName;
fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login, const std::string & fileName) const
+int FILES_STORE::RestoreUserStat(STG::UserStat * stat, const std::string & login, const std::string & fileName) const
{
CONFIGFILE cf(fileName);
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const
+int FILES_STORE::SaveUserConf(const STG::UserConf & conf, const std::string & login) const
{
std::string fileName;
fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
+int FILES_STORE::SaveUserStat(const STG::UserStat & stat, const std::string & login) const
{
std::string fileName;
fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
}
//-----------------------------------------------------------------------------
int FILES_STORE::WriteUserDisconnect(const std::string & login,
- const DIR_TRAFF & monthUp,
- const DIR_TRAFF & monthDown,
- const DIR_TRAFF & sessionUp,
- const DIR_TRAFF & sessionDown,
+ const STG::DirTraff & monthUp,
+ const STG::DirTraff & monthDown,
+ const STG::DirTraff & sessionUp,
+ const STG::DirTraff & sessionDown,
double cash,
double freeMb,
const std::string & reason) const
return WriteLog2String(logStr.str(), login);
}
//-----------------------------------------------------------------------------
-int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const std::string & login) const
+int FILES_STORE::SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string & login) const
{
// Classic stats
std::string stat1;
return 0;
}
//-----------------------------------------------------------------------------*/
-int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
+int FILES_STORE::SaveAdmin(const STG::AdminConf & ac) const
{
std::string fileName;
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const
+int FILES_STORE::RestoreAdmin(STG::AdminConf * ac, const std::string & login) const
{
std::string fileName;
strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const
+int FILES_STORE::RestoreTariff(STG::TariffData * td, const std::string & tariffName) const
{
std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
CONFIGFILE conf(fileName);
return -1;
}
-td->tariffConf.traffType = TARIFF::StringToTraffType(str);
+td->tariffConf.traffType = STG::Tariff::parseTraffType(str);
if (conf.ReadString("Period", &str, "month") < 0)
- td->tariffConf.period = TARIFF::MONTH;
+ td->tariffConf.period = STG::Tariff::MONTH;
else
- td->tariffConf.period = TARIFF::StringToPeriod(str);
+ td->tariffConf.period = STG::Tariff::parsePeriod(str);
if (conf.ReadString("ChangePolicy", &str, "allow") < 0)
- td->tariffConf.changePolicy = TARIFF::ALLOW;
+ td->tariffConf.changePolicy = STG::Tariff::ALLOW;
else
- td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(str);
+ td->tariffConf.changePolicy = STG::Tariff::parseChangePolicy(str);
-if (conf.ReadTime("ChangePolicyTimeout", &td->tariffConf.changePolicyTimeout, 0) < 0)
- {
- STG_LOCKER lock(&mutex);
- errorStr = "Cannot read tariff " + tariffName + ". Parameter ChangePolicyTimeout";
- printfd(__FILE__, "FILES_STORE::RestoreTariff - changepolicytimeout read failed for tariff '%s'\n", tariffName.c_str());
- return -1;
- }
+conf.ReadTime("ChangePolicyTimeout", &td->tariffConf.changePolicyTimeout, 0);
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const
+int FILES_STORE::SaveTariff(const STG::TariffData & td, const std::string & tariffName) const
{
std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
cf.WriteDouble("Fee", td.tariffConf.fee);
cf.WriteDouble("Free", td.tariffConf.free);
- cf.WriteString("TraffType", TARIFF::TraffTypeToString(td.tariffConf.traffType));
- cf.WriteString("Period", TARIFF::PeriodToString(td.tariffConf.period));
- cf.WriteString("ChangePolicy", TARIFF::ChangePolicyToString(td.tariffConf.changePolicy));
+ cf.WriteString("TraffType", STG::Tariff::toString(td.tariffConf.traffType));
+ cf.WriteString("Period", STG::Tariff::toString(td.tariffConf.period));
+ cf.WriteString("ChangePolicy", STG::Tariff::toString(td.tariffConf.changePolicy));
+ cf.WriteTime("ChangePolicyTimeout", td.tariffConf.changePolicyTimeout);
+ }
+
+return 0;
+}
+//-----------------------------------------------------------------------------*/
+int FILES_STORE::AddService(const std::string & name) const
+{
+std::string fileName;
+strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str());
+
+if (Touch(fileName))
+ {
+ STG_LOCKER lock(&mutex);
+ errorStr = "Cannot create file " + fileName;
+ printfd(__FILE__, "FILES_STORE::AddService - failed to add service '%s'\n", name.c_str());
+ return -1;
+ }
+
+return 0;
+}
+//-----------------------------------------------------------------------------*/
+int FILES_STORE::DelService(const std::string & name) const
+{
+std::string fileName;
+strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str());
+if (unlink(fileName.c_str()))
+ {
+ STG_LOCKER lock(&mutex);
+ errorStr = "unlink failed. Message: '";
+ errorStr += strerror(errno);
+ errorStr += "'";
+ printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
+ }
+return 0;
+}
+//-----------------------------------------------------------------------------*/
+int FILES_STORE::SaveService(const STG::ServiceConf & conf) const
+{
+std::string fileName;
+
+strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), conf.name.c_str());
+
+ {
+ CONFIGFILE cf(fileName, true);
+
+ int e = cf.Error();
+
+ if (e)
+ {
+ STG_LOCKER lock(&mutex);
+ errorStr = "Cannot write service " + conf.name + ". " + fileName;
+ printfd(__FILE__, "FILES_STORE::SaveService - failed to save service '%s'\n", conf.name.c_str());
+ return -1;
+ }
+
+ cf.WriteString("name", conf.name);
+ cf.WriteString("comment", conf.comment);
+ cf.WriteDouble("cost", conf.cost);
+ cf.WriteInt("pay_day", conf.payDay);
+ }
+
+return 0;
+}
+//-----------------------------------------------------------------------------
+int FILES_STORE::RestoreService(STG::ServiceConf * conf, const std::string & name) const
+{
+std::string fileName;
+strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str());
+CONFIGFILE cf(fileName);
+
+if (cf.Error())
+ {
+ STG_LOCKER lock(&mutex);
+ errorStr = "Cannot open " + fileName;
+ printfd(__FILE__, "FILES_STORE::RestoreService - failed to restore service '%s'\n", name.c_str());
+ return -1;
+ }
+
+if (cf.ReadString("name", &conf->name, name))
+ {
+ STG_LOCKER lock(&mutex);
+ errorStr = "Error in parameter 'name'";
+ printfd(__FILE__, "FILES_STORE::RestoreService - name read failed for service '%s'\n", name.c_str());
+ return -1;
+ }
+
+if (cf.ReadString("comment", &conf->comment, ""))
+ {
+ STG_LOCKER lock(&mutex);
+ errorStr = "Error in parameter 'comment'";
+ printfd(__FILE__, "FILES_STORE::RestoreService - comment read failed for service '%s'\n", name.c_str());
+ return -1;
+ }
+
+if (cf.ReadDouble("cost", &conf->cost, 0.0))
+ {
+ STG_LOCKER lock(&mutex);
+ errorStr = "Error in parameter 'cost'";
+ printfd(__FILE__, "FILES_STORE::RestoreService - cost read failed for service '%s'\n", name.c_str());
+ return -1;
+ }
+
+unsigned short value = 0;
+if (cf.ReadUShortInt("pay_day", &value, 0))
+ {
+ STG_LOCKER lock(&mutex);
+ errorStr = "Error in parameter 'pay_day'";
+ printfd(__FILE__, "FILES_STORE::RestoreService - pay day read failed for service '%s'\n", name.c_str());
+ return -1;
}
+conf->payDay = value;
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree,
+int FILES_STORE::WriteDetailedStat(const STG::TraffStat & statTree,
time_t lastStat,
const std::string & login) const
{
return -1;
}
-std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
-stIter = statTree.begin();
+auto stIter = statTree.begin();
while (stIter != statTree.end())
{
- std::string u, d;
- x2str(stIter->second.up, u);
- x2str(stIter->second.down, d);
+ const auto u = std::to_string(stIter->second.up);
+ const auto d = std::to_string(stIter->second.down);
#ifdef TRAFF_STAT_WITH_PORTS
if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
inet_ntostring(stIter->first.ip).c_str(),
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
+int FILES_STORE::AddMessage(STG::Message * msg, const std::string & login) const
{
std::string fn;
std::string dn;
return EditMessage(*msg, login);
}
//-----------------------------------------------------------------------------
-int FILES_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const
+int FILES_STORE::EditMessage(const STG::Message & msg, const std::string & login) const
{
std::string fileName;
if (access(fileName.c_str(), F_OK) != 0)
{
- std::string idstr;
- x2str(msg.header.id, idstr);
STG_LOCKER lock(&mutex);
errorStr = "Message for user \'";
errorStr += login + "\' with ID \'";
- errorStr += idstr + "\' does not exist.";
+ errorStr += std::to_string(msg.header.id) + "\' does not exist.";
printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
-int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const
+int FILES_STORE::GetMessage(uint64_t id, STG::Message * msg, const std::string & login) const
{
std::string fn;
strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
return unlink(fn.c_str());
}
//-----------------------------------------------------------------------------
-int FILES_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const
+int FILES_STORE::GetMessageHdrs(std::vector<STG::Message::Header> * hdrsList, const std::string & login) const
{
std::string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/");
continue;
}
- STG_MSG_HDR hdr;
+ STG::Message::Header hdr;
if (ReadMessage(dn + messages[i], &hdr, NULL))
{
return -1;
}
//-----------------------------------------------------------------------------
int FILES_STORE::ReadMessage(const std::string & fileName,
- STG_MSG_HDR * hdr,
+ STG::Message::Header * hdr,
std::string * text) const
{
FILE * msgFile;