X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/cf3954a35eaddf462fbe1247f54c0b4b9f9eac80..6fd1ed115d0f47b3e88f54c89fa3295446f34851:/projects/stargazer/plugins/store/files/file_store.cpp diff --git a/projects/stargazer/plugins/store/files/file_store.cpp b/projects/stargazer/plugins/store/files/file_store.cpp index 689f5fab..3fd9e6d9 100644 --- a/projects/stargazer/plugins/store/files/file_store.cpp +++ b/projects/stargazer/plugins/store/files/file_store.cpp @@ -28,20 +28,7 @@ #define _GNU_SOURCE #endif -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include +#include "file_store.h" #include "stg/common.h" #include "stg/user_ips.h" @@ -51,8 +38,25 @@ #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 +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include #define DELETED_USERS_DIR "deleted_users" @@ -66,25 +70,26 @@ const int pt_mega = 1024 * 1024; //----------------------------------------------------------------------------- namespace { -PLUGIN_CREATOR 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() : settings(NULL), - errorStr(), - workDir(), - usersDir(), - adminsDir(), - tariffsDir(), statMode(0), statUID(0), statGID(0), @@ -99,13 +104,13 @@ FILES_STORE_SETTINGS::FILES_STORE_SETTINGS() { } //----------------------------------------------------------------------------- -int FILES_STORE_SETTINGS::ParseOwner(const std::vector & moduleParams, const std::string & owner, uid_t * uid) +int FILES_STORE_SETTINGS::ParseOwner(const std::vector & moduleParams, const std::string & owner, uid_t * uid) { -PARAM_VALUE pv; +STG::ParamValue pv; pv.param = owner; -std::vector::const_iterator pvi; +std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); -if (pvi == moduleParams.end()) +if (pvi == moduleParams.end() || pvi->value.empty()) { errorStr = "Parameter \'" + owner + "\' not found."; printfd(__FILE__, "%s\n", errorStr.c_str()); @@ -120,13 +125,13 @@ if (User2UID(pvi->value[0].c_str(), uid) < 0) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE_SETTINGS::ParseGroup(const std::vector & moduleParams, const std::string & group, gid_t * gid) +int FILES_STORE_SETTINGS::ParseGroup(const std::vector & moduleParams, const std::string & group, gid_t * gid) { -PARAM_VALUE pv; +STG::ParamValue pv; pv.param = group; -std::vector::const_iterator pvi; +std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); -if (pvi == moduleParams.end()) +if (pvi == moduleParams.end() || pvi->value.empty()) { errorStr = "Parameter \'" + group + "\' not found."; printfd(__FILE__, "%s\n", errorStr.c_str()); @@ -158,13 +163,13 @@ errorStr = "Incorrect value \'" + value + "\'."; return -1; } //----------------------------------------------------------------------------- -int FILES_STORE_SETTINGS::ParseMode(const std::vector & moduleParams, const std::string & modeStr, mode_t * mode) +int FILES_STORE_SETTINGS::ParseMode(const std::vector & moduleParams, const std::string & modeStr, mode_t * mode) { -PARAM_VALUE pv; +STG::ParamValue pv; pv.param = modeStr; -std::vector::const_iterator pvi; +std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); -if (pvi == moduleParams.end()) +if (pvi == moduleParams.end() || pvi->value.empty()) { errorStr = "Parameter \'" + modeStr + "\' not found."; printfd(__FILE__, "%s\n", errorStr.c_str()); @@ -179,7 +184,7 @@ if (Str2Mode(pvi->value[0].c_str(), mode) < 0) 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; @@ -202,11 +207,11 @@ if (ParseGroup(s.moduleParams, "UserLogGroup", &userLogGID) < 0) if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0) return -1; -std::vector::const_iterator pvi; -PARAM_VALUE pv; +std::vector::const_iterator pvi; +STG::ParamValue pv; pv.param = "RemoveBak"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) { removeBak = true; } @@ -221,7 +226,7 @@ else pv.param = "ReadBak"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) { readBak = false; } @@ -236,7 +241,7 @@ else pv.param = "WorkDir"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) { errorStr = "Parameter \'WorkDir\' not found."; printfd(__FILE__, "Parameter 'WorkDir' not found\n"); @@ -249,8 +254,33 @@ if (workDir.size() && workDir[workDir.size() - 1] == '/') 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; } @@ -340,12 +370,8 @@ return mode; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- 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); @@ -415,6 +441,24 @@ STG_LOCKER lock(&mutex); tariffList->swap(files); +return 0; +} +//----------------------------------------------------------------------------- +int FILES_STORE::GetServicesList(std::vector * list) const +{ +std::vector 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; } //----------------------------------------------------------------------------- @@ -558,7 +602,7 @@ else 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"; @@ -573,7 +617,7 @@ if (RestoreUserConf(conf, login, fileName)) 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(); @@ -618,10 +662,9 @@ if (conf->tariffName.empty()) 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) { @@ -630,7 +673,6 @@ 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) { @@ -684,7 +726,7 @@ if (cf.ReadDouble("Credit", &conf->credit, 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"; @@ -700,7 +742,7 @@ if (RestoreUserStat(stat, login, fileName)) 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); @@ -791,7 +833,7 @@ if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0) 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"; @@ -847,7 +889,7 @@ cfstat.WriteString("IP", ipStr.str()); 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"; @@ -989,10 +1031,10 @@ return WriteLog2String(logStr, login); } //----------------------------------------------------------------------------- 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 @@ -1024,7 +1066,7 @@ logStr << " freeMb: \'" 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; @@ -1112,7 +1154,7 @@ if (unlink(fileName.c_str())) return 0; } //-----------------------------------------------------------------------------*/ -int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const +int FILES_STORE::SaveAdmin(const STG::AdminConf & ac) const { std::string fileName; @@ -1167,7 +1209,7 @@ strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login 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()); @@ -1331,7 +1373,7 @@ if (unlink(fileName.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); @@ -1464,33 +1506,23 @@ if (conf.ReadString("TraffType", &str, "") < 0) return -1; } -if (!strcasecmp(str.c_str(), "up")) - td->tariffConf.traffType = TRAFF_UP; -else - if (!strcasecmp(str.c_str(), "down")) - td->tariffConf.traffType = TRAFF_DOWN; - else - if (!strcasecmp(str.c_str(), "up+down")) - td->tariffConf.traffType = TRAFF_UP_DOWN; - else - if (!strcasecmp(str.c_str(), "max")) - td->tariffConf.traffType = TRAFF_MAX; - else - { - STG_LOCKER lock(&mutex); - errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect"; - printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str()); - return -1; - } +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 = STG::Tariff::ALLOW; +else + td->tariffConf.changePolicy = STG::Tariff::parseChangePolicy(str); + +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"; @@ -1546,30 +1578,126 @@ 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", 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); + } - switch (td.tariffConf.traffType) +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) { - case TRAFF_UP: - cf.WriteString("TraffType", "up"); - break; - case TRAFF_DOWN: - cf.WriteString("TraffType", "down"); - break; - case TRAFF_UP_DOWN: - cf.WriteString("TraffType", "up+down"); - break; - case TRAFF_MAX: - cf.WriteString("TraffType", "max"); - break; + 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("Period", TARIFF::PeriodToString(td.tariffConf.period)); + 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 & statTree, +int FILES_STORE::WriteDetailedStat(const STG::TraffStat & statTree, time_t lastStat, const std::string & login) const { @@ -1701,14 +1829,12 @@ if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n", return -1; } -std::map::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(), @@ -1761,7 +1887,7 @@ if (e) 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; @@ -1801,7 +1927,7 @@ if (Touch(fn)) 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; @@ -1810,12 +1936,10 @@ strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), 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; } @@ -1864,7 +1988,7 @@ if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0) 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); @@ -1880,7 +2004,7 @@ strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login return unlink(fn.c_str()); } //----------------------------------------------------------------------------- -int FILES_STORE::GetMessageHdrs(std::vector * hdrsList, const std::string & login) const +int FILES_STORE::GetMessageHdrs(std::vector * hdrsList, const std::string & login) const { std::string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/"); @@ -1908,7 +2032,7 @@ for (unsigned i = 0; i < messages.size(); i++) continue; } - STG_MSG_HDR hdr; + STG::Message::Header hdr; if (ReadMessage(dn + messages[i], &hdr, NULL)) { return -1; @@ -1933,7 +2057,7 @@ return 0; } //----------------------------------------------------------------------------- int FILES_STORE::ReadMessage(const std::string & fileName, - STG_MSG_HDR * hdr, + STG::Message::Header * hdr, std::string * text) const { FILE * msgFile;