2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 $Date: 2010/10/07 19:53:11 $
31 #include "file_store.h"
33 #include "stg/common.h"
34 #include "stg/user_ips.h"
35 #include "stg/user_conf.h"
36 #include "stg/user_stat.h"
37 #include "stg/const.h"
38 #include "stg/blowfish.h"
39 #include "stg/logger.h"
40 #include "stg/locker.h"
41 #include "stg/admin_conf.h"
42 #include "stg/tariff.h"
43 #include "stg/tariff_conf.h"
44 #include "stg/service_conf.h"
61 #define DELETED_USERS_DIR "deleted_users"
63 #define adm_enc_passwd "cjeifY8m3"
65 int GetFileList(std::vector<std::string> * fileList, const std::string & directory, mode_t mode, const std::string & ext);
67 const int pt_mega = 1024 * 1024;
68 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
74 bool CheckAndCreate(const std::string & dir, mode_t mode)
76 if (access(dir.c_str(), F_OK) == 0)
78 if (mkdir(dir.c_str(), mode) == 0)
85 extern "C" STG::Store* GetStore()
87 static FILES_STORE plugin;
90 //-----------------------------------------------------------------------------
91 FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
106 //-----------------------------------------------------------------------------
107 int FILES_STORE_SETTINGS::ParseOwner(const std::vector<STG::ParamValue> & moduleParams, const std::string & owner, uid_t * uid)
111 std::vector<STG::ParamValue>::const_iterator pvi;
112 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
113 if (pvi == moduleParams.end() || pvi->value.empty())
115 errorStr = "Parameter \'" + owner + "\' not found.";
116 printfd(__FILE__, "%s\n", errorStr.c_str());
119 if (User2UID(pvi->value[0].c_str(), uid) < 0)
121 errorStr = "Parameter \'" + owner + "\': Unknown user \'" + pvi->value[0] + "\'";
122 printfd(__FILE__, "%s\n", errorStr.c_str());
127 //-----------------------------------------------------------------------------
128 int FILES_STORE_SETTINGS::ParseGroup(const std::vector<STG::ParamValue> & moduleParams, const std::string & group, gid_t * gid)
132 std::vector<STG::ParamValue>::const_iterator pvi;
133 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
134 if (pvi == moduleParams.end() || pvi->value.empty())
136 errorStr = "Parameter \'" + group + "\' not found.";
137 printfd(__FILE__, "%s\n", errorStr.c_str());
140 if (Group2GID(pvi->value[0].c_str(), gid) < 0)
142 errorStr = "Parameter \'" + group + "\': Unknown group \'" + pvi->value[0] + "\'";
143 printfd(__FILE__, "%s\n", errorStr.c_str());
148 //-----------------------------------------------------------------------------
149 int FILES_STORE_SETTINGS::ParseYesNo(const std::string & value, bool * val)
151 if (0 == strcasecmp(value.c_str(), "yes"))
156 if (0 == strcasecmp(value.c_str(), "no"))
162 errorStr = "Incorrect value \'" + value + "\'.";
165 //-----------------------------------------------------------------------------
166 int FILES_STORE_SETTINGS::ParseMode(const std::vector<STG::ParamValue> & moduleParams, const std::string & modeStr, mode_t * mode)
170 std::vector<STG::ParamValue>::const_iterator pvi;
171 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
172 if (pvi == moduleParams.end() || pvi->value.empty())
174 errorStr = "Parameter \'" + modeStr + "\' not found.";
175 printfd(__FILE__, "%s\n", errorStr.c_str());
178 if (Str2Mode(pvi->value[0].c_str(), mode) < 0)
180 errorStr = "Parameter \'" + modeStr + "\': Incorrect mode \'" + pvi->value[0] + "\'";
181 printfd(__FILE__, "%s\n", errorStr.c_str());
186 //-----------------------------------------------------------------------------
187 int FILES_STORE_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
189 if (ParseOwner(s.moduleParams, "StatOwner", &statUID) < 0)
191 if (ParseGroup(s.moduleParams, "StatGroup", &statGID) < 0)
193 if (ParseMode(s.moduleParams, "StatMode", &statMode) < 0)
196 if (ParseOwner(s.moduleParams, "ConfOwner", &confUID) < 0)
198 if (ParseGroup(s.moduleParams, "ConfGroup", &confGID) < 0)
200 if (ParseMode(s.moduleParams, "ConfMode", &confMode) < 0)
203 if (ParseOwner(s.moduleParams, "UserLogOwner", &userLogUID) < 0)
205 if (ParseGroup(s.moduleParams, "UserLogGroup", &userLogGID) < 0)
207 if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0)
210 std::vector<STG::ParamValue>::const_iterator pvi;
212 pv.param = "RemoveBak";
213 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
214 if (pvi == s.moduleParams.end() || pvi->value.empty())
220 if (ParseYesNo(pvi->value[0], &removeBak))
222 printfd(__FILE__, "Cannot parse parameter 'RemoveBak'\n");
227 pv.param = "ReadBak";
228 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
229 if (pvi == s.moduleParams.end() || pvi->value.empty())
235 if (ParseYesNo(pvi->value[0], &readBak))
237 printfd(__FILE__, "Cannot parse parameter 'ReadBak'\n");
242 pv.param = "WorkDir";
243 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
244 if (pvi == s.moduleParams.end() || pvi->value.empty())
246 errorStr = "Parameter \'WorkDir\' not found.";
247 printfd(__FILE__, "Parameter 'WorkDir' not found\n");
251 workDir = pvi->value[0];
252 if (workDir.size() && workDir[workDir.size() - 1] == '/')
254 workDir.resize(workDir.size() - 1);
256 usersDir = workDir + "/users/";
257 if (!CheckAndCreate(usersDir, GetConfModeDir()))
259 errorStr = usersDir + " doesn't exist. Failed to create.";
260 printfd(__FILE__, "%s\n", errorStr.c_str());
263 tariffsDir = workDir + "/tariffs/";
264 if (!CheckAndCreate(tariffsDir, GetConfModeDir()))
266 errorStr = tariffsDir + " doesn't exist. Failed to create.";
267 printfd(__FILE__, "%s\n", errorStr.c_str());
270 adminsDir = workDir + "/admins/";
271 if (!CheckAndCreate(adminsDir, GetConfModeDir()))
273 errorStr = adminsDir + " doesn't exist. Failed to create.";
274 printfd(__FILE__, "%s\n", errorStr.c_str());
277 servicesDir = workDir + "/services/";
278 if (!CheckAndCreate(servicesDir, GetConfModeDir()))
280 errorStr = servicesDir + " doesn't exist. Failed to create.";
281 printfd(__FILE__, "%s\n", errorStr.c_str());
287 //-----------------------------------------------------------------------------
288 const std::string & FILES_STORE_SETTINGS::GetStrError() const
292 //-----------------------------------------------------------------------------
293 int FILES_STORE_SETTINGS::User2UID(const char * user, uid_t * uid)
299 errorStr = std::string("User \'") + std::string(user) + std::string("\' not found in system.");
300 printfd(__FILE__, "%s\n", errorStr.c_str());
307 //-----------------------------------------------------------------------------
308 int FILES_STORE_SETTINGS::Group2GID(const char * gr, gid_t * gid)
314 errorStr = std::string("Group \'") + std::string(gr) + std::string("\' not found in system.");
315 printfd(__FILE__, "%s\n", errorStr.c_str());
322 //-----------------------------------------------------------------------------
323 int FILES_STORE_SETTINGS::Str2Mode(const char * str, mode_t * mode)
330 errorStr = std::string("Error parsing mode \'") + str + std::string("\'");
331 printfd(__FILE__, "%s\n", errorStr.c_str());
335 for (int i = 0; i < 3; i++)
336 if (str[i] > '7' || str[i] < '0')
338 errorStr = std::string("Error parsing mode \'") + str + std::string("\'");
339 printfd(__FILE__, "%s\n", errorStr.c_str());
347 *mode = ((mode_t)c) + ((mode_t)b << 3) + ((mode_t)a << 6);
351 //-----------------------------------------------------------------------------
352 mode_t FILES_STORE_SETTINGS::GetStatModeDir() const
354 mode_t mode = statMode;
355 if (statMode & S_IRUSR) mode |= S_IXUSR;
356 if (statMode & S_IRGRP) mode |= S_IXGRP;
357 if (statMode & S_IROTH) mode |= S_IXOTH;
360 //-----------------------------------------------------------------------------
361 mode_t FILES_STORE_SETTINGS::GetConfModeDir() const
363 mode_t mode = confMode;
364 if (confMode & S_IRUSR) mode |= S_IXUSR;
365 if (confMode & S_IRGRP) mode |= S_IXGRP;
366 if (confMode & S_IROTH) mode |= S_IXOTH;
369 //-----------------------------------------------------------------------------
370 //-----------------------------------------------------------------------------
371 //-----------------------------------------------------------------------------
372 FILES_STORE::FILES_STORE()
373 : version("file_store v.1.04"),
374 logger(STG::PluginLogger::get("store_files"))
376 pthread_mutexattr_t attr;
377 pthread_mutexattr_init(&attr);
378 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
379 pthread_mutex_init(&mutex, &attr);
381 //-----------------------------------------------------------------------------
382 int FILES_STORE::ParseSettings()
384 int ret = storeSettings.ParseSettings(settings);
387 STG_LOCKER lock(&mutex);
388 errorStr = storeSettings.GetStrError();
392 //-----------------------------------------------------------------------------
393 int FILES_STORE::GetUsersList(std::vector<std::string> * userList) const
395 std::vector<std::string> files;
397 if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, ""))
399 STG_LOCKER lock(&mutex);
400 errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + std::string(strerror(errno));
404 STG_LOCKER lock(&mutex);
406 userList->swap(files);
410 //-----------------------------------------------------------------------------
411 int FILES_STORE::GetAdminsList(std::vector<std::string> * adminList) const
413 std::vector<std::string> files;
415 if (GetFileList(&files, storeSettings.GetAdminsDir(), S_IFREG, ".adm"))
417 STG_LOCKER lock(&mutex);
418 errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + std::string(strerror(errno));
422 STG_LOCKER lock(&mutex);
424 adminList->swap(files);
428 //-----------------------------------------------------------------------------
429 int FILES_STORE::GetTariffsList(std::vector<std::string> * tariffList) const
431 std::vector<std::string> files;
433 if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf"))
435 STG_LOCKER lock(&mutex);
436 errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + std::string(strerror(errno));
440 STG_LOCKER lock(&mutex);
442 tariffList->swap(files);
446 //-----------------------------------------------------------------------------
447 int FILES_STORE::GetServicesList(std::vector<std::string> * list) const
449 std::vector<std::string> files;
451 if (GetFileList(&files, storeSettings.GetServicesDir(), S_IFREG, ".serv"))
453 STG_LOCKER lock(&mutex);
454 errorStr = "Failed to open '" + storeSettings.GetServicesDir() + "': " + std::string(strerror(errno));
458 STG_LOCKER lock(&mutex);
464 //-----------------------------------------------------------------------------
465 int FILES_STORE::RemoveDir(const char * path) const
467 DIR * d = opendir(path);
471 errorStr = "failed to open dir. Message: '";
472 errorStr += strerror(errno);
474 printfd(__FILE__, "FILE_STORE::RemoveDir() - Failed to open dir '%s': '%s'\n", path, strerror(errno));
479 while ((entry = readdir(d)))
481 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
484 std::string str = path;
485 str += "/" + std::string(entry->d_name);
488 if (stat(str.c_str(), &st))
491 if ((st.st_mode & S_IFREG))
493 if (unlink(str.c_str()))
495 STG_LOCKER lock(&mutex);
496 errorStr = "unlink failed. Message: '";
497 errorStr += strerror(errno);
499 printfd(__FILE__, "FILES_STORE::RemoveDir() - unlink failed. Message: '%s'\n", strerror(errno));
505 if (!(st.st_mode & S_IFDIR))
507 if (RemoveDir(str.c_str()))
520 STG_LOCKER lock(&mutex);
521 errorStr = "rmdir failed. Message: '";
522 errorStr += strerror(errno);
524 printfd(__FILE__, "FILES_STORE::RemoveDir() - rmdir failed. Message: '%s'\n", strerror(errno));
530 //-----------------------------------------------------------------------------
531 int FILES_STORE::AddUser(const std::string & login) const
533 std::string fileName;
535 strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str());
537 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
539 STG_LOCKER lock(&mutex);
540 errorStr = std::string("mkdir failed. Message: '") + strerror(errno) + "'";
541 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
545 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
548 STG_LOCKER lock(&mutex);
549 errorStr = "Cannot create file \"" + fileName + "\'";
550 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
554 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
557 STG_LOCKER lock(&mutex);
558 errorStr = "Cannot create file \"" + fileName + "\'";
559 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
564 //-----------------------------------------------------------------------------
565 int FILES_STORE::DelUser(const std::string & login) const
568 std::string dirName1;
570 strprintf(&dirName, "%s/%s", storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR);
571 if (access(dirName.c_str(), F_OK) != 0)
573 if (mkdir(dirName.c_str(), 0700) != 0)
575 STG_LOCKER lock(&mutex);
576 errorStr = "Directory '" + dirName + "' cannot be created.";
577 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
582 if (access(dirName.c_str(), F_OK) == 0)
584 strprintf(&dirName, "%s/%s/%s.%lu", storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR, login.c_str(), time(NULL));
585 strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
586 if (rename(dirName1.c_str(), dirName.c_str()))
588 STG_LOCKER lock(&mutex);
589 errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
590 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
596 strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
597 if (RemoveDir(dirName.c_str()))
604 //-----------------------------------------------------------------------------
605 int FILES_STORE::RestoreUserConf(STG::UserConf * conf, const std::string & login) const
607 std::string fileName;
608 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
609 if (RestoreUserConf(conf, login, fileName))
611 if (!storeSettings.GetReadBak())
615 return RestoreUserConf(conf, login, fileName + ".bak");
619 //-----------------------------------------------------------------------------
620 int FILES_STORE::RestoreUserConf(STG::UserConf * conf, const std::string & login, const std::string & fileName) const
622 CONFIGFILE cf(fileName);
627 STG_LOCKER lock(&mutex);
628 errorStr = "User \'" + login + "\' data not read.";
629 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
633 if (cf.ReadString("Password", &conf->password, "") < 0)
635 STG_LOCKER lock(&mutex);
636 errorStr = "User \'" + login + "\' data not read. Parameter Password.";
637 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
640 if (conf->password.empty())
642 STG_LOCKER lock(&mutex);
643 errorStr = "User \'" + login + "\' password is blank.";
644 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
648 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
650 STG_LOCKER lock(&mutex);
651 errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
652 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
655 if (conf->tariffName.empty())
657 STG_LOCKER lock(&mutex);
658 errorStr = "User \'" + login + "\' tariff is blank.";
659 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
664 cf.ReadString("IP", &ipStr, "?");
667 conf->ips = STG::UserIPs::parse(ipStr);
669 catch (const std::string & s)
671 STG_LOCKER lock(&mutex);
672 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
673 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
677 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
679 STG_LOCKER lock(&mutex);
680 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
681 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
685 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
687 STG_LOCKER lock(&mutex);
688 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
689 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
693 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
695 STG_LOCKER lock(&mutex);
696 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
697 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
701 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
702 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
703 cf.ReadString("TariffChange", &conf->nextTariff, "");
704 cf.ReadString("Group", &conf->group, "");
705 cf.ReadString("RealName", &conf->realName, "");
706 cf.ReadString("Address", &conf->address, "");
707 cf.ReadString("Phone", &conf->phone, "");
708 cf.ReadString("Note", &conf->note, "");
709 cf.ReadString("email", &conf->email, "");
711 char userdataName[12];
712 for (int i = 0; i < USERDATA_NUM; i++)
714 snprintf(userdataName, 12, "Userdata%d", i);
715 cf.ReadString(userdataName, &conf->userdata[i], "");
718 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
720 STG_LOCKER lock(&mutex);
721 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
722 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
728 //-----------------------------------------------------------------------------
729 int FILES_STORE::RestoreUserStat(STG::UserStat * stat, const std::string & login) const
731 std::string fileName;
732 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
734 if (RestoreUserStat(stat, login, fileName))
736 if (!storeSettings.GetReadBak())
740 return RestoreUserStat(stat, login, fileName + ".bak");
744 //-----------------------------------------------------------------------------
745 int FILES_STORE::RestoreUserStat(STG::UserStat * stat, const std::string & login, const std::string & fileName) const
747 CONFIGFILE cf(fileName);
753 STG_LOCKER lock(&mutex);
754 errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
755 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
761 for (int i = 0; i < DIR_NUM; i++)
764 snprintf(s, 22, "D%d", i);
765 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
767 STG_LOCKER lock(&mutex);
768 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
769 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
772 stat->monthDown[i] = traff;
774 snprintf(s, 22, "U%d", i);
775 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
777 STG_LOCKER lock(&mutex);
778 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
779 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
782 stat->monthUp[i] = traff;
785 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
787 STG_LOCKER lock(&mutex);
788 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
789 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
793 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
795 STG_LOCKER lock(&mutex);
796 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
797 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
801 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
803 STG_LOCKER lock(&mutex);
804 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
805 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
809 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
811 STG_LOCKER lock(&mutex);
812 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
813 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
817 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
819 STG_LOCKER lock(&mutex);
820 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
821 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
825 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
827 STG_LOCKER lock(&mutex);
828 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
829 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
835 //-----------------------------------------------------------------------------
836 int FILES_STORE::SaveUserConf(const STG::UserConf & conf, const std::string & login) const
838 std::string fileName;
839 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
841 CONFIGFILE cfstat(fileName, true);
843 int e = cfstat.Error();
847 STG_LOCKER lock(&mutex);
848 errorStr = std::string("User \'") + login + "\' conf not written\n";
849 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
853 e = chmod(fileName.c_str(), storeSettings.GetConfMode());
854 e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID());
858 STG_LOCKER lock(&mutex);
859 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
862 cfstat.WriteString("Password", conf.password);
863 cfstat.WriteInt ("Passive", conf.passive);
864 cfstat.WriteInt ("Down", conf.disabled);
865 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
866 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
867 cfstat.WriteString("Tariff", conf.tariffName);
868 cfstat.WriteString("Address", conf.address);
869 cfstat.WriteString("Phone", conf.phone);
870 cfstat.WriteString("Email", conf.email);
871 cfstat.WriteString("Note", conf.note);
872 cfstat.WriteString("RealName", conf.realName);
873 cfstat.WriteString("Group", conf.group);
874 cfstat.WriteDouble("Credit", conf.credit);
875 cfstat.WriteString("TariffChange", conf.nextTariff);
877 char userdataName[12];
878 for (int i = 0; i < USERDATA_NUM; i++)
880 snprintf(userdataName, 12, "Userdata%d", i);
881 cfstat.WriteString(userdataName, conf.userdata[i]);
883 cfstat.WriteInt("CreditExpire", conf.creditExpire);
885 std::ostringstream ipStr;
887 cfstat.WriteString("IP", ipStr.str());
891 //-----------------------------------------------------------------------------
892 int FILES_STORE::SaveUserStat(const STG::UserStat & stat, const std::string & login) const
894 std::string fileName;
895 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
898 CONFIGFILE cfstat(fileName, true);
899 int e = cfstat.Error();
903 STG_LOCKER lock(&mutex);
904 errorStr = std::string("User \'") + login + "\' stat not written\n";
905 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
909 for (int i = 0; i < DIR_NUM; i++)
912 snprintf(s, 22, "D%d", i);
913 cfstat.WriteInt(s, stat.monthDown[i]);
914 snprintf(s, 22, "U%d", i);
915 cfstat.WriteInt(s, stat.monthUp[i]);
918 cfstat.WriteDouble("Cash", stat.cash);
919 cfstat.WriteDouble("FreeMb", stat.freeMb);
920 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
921 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
922 cfstat.WriteInt("PassiveTime", stat.passiveTime);
923 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
926 int e = chmod(fileName.c_str(), storeSettings.GetStatMode());
927 e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID());
931 STG_LOCKER lock(&mutex);
932 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
937 //-----------------------------------------------------------------------------
938 int FILES_STORE::WriteLogString(const std::string & str, const std::string & login) const
941 time_t tm = time(NULL);
942 std::string fileName;
943 fileName = storeSettings.GetUsersDir() + "/" + login + "/log";
944 f = fopen(fileName.c_str(), "at");
948 fprintf(f, "%s", LogDate(tm));
950 fprintf(f, "%s", str.c_str());
956 STG_LOCKER lock(&mutex);
957 errorStr = "Cannot open \'" + fileName + "\'";
958 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
962 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
963 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
967 STG_LOCKER lock(&mutex);
968 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
973 //-----------------------------------------------------------------------------
974 int FILES_STORE::WriteLog2String(const std::string & str, const std::string & login) const
977 time_t tm = time(NULL);
978 std::string fileName;
979 fileName = storeSettings.GetUsersDir() + "/" + login + "/log2";
980 f = fopen(fileName.c_str(), "at");
984 fprintf(f, "%s", LogDate(tm));
986 fprintf(f, "%s", str.c_str());
992 STG_LOCKER lock(&mutex);
993 errorStr = "Cannot open \'" + fileName + "\'";
994 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
998 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
999 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1003 STG_LOCKER lock(&mutex);
1004 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1009 //-----------------------------------------------------------------------------
1010 int FILES_STORE::WriteUserChgLog(const std::string & login,
1011 const std::string & admLogin,
1013 const std::string & paramName,
1014 const std::string & oldValue,
1015 const std::string & newValue,
1016 const std::string & message) const
1018 std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1019 + paramName + "\' parameter changed from \'" + oldValue +
1020 "\' to \'" + newValue + "\'. " + message;
1022 return WriteLogString(userLogMsg, login);
1024 //-----------------------------------------------------------------------------
1025 int FILES_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
1027 std::string logStr = "Connect, " + inet_ntostring(ip);
1028 if (WriteLogString(logStr, login))
1030 return WriteLog2String(logStr, login);
1032 //-----------------------------------------------------------------------------
1033 int FILES_STORE::WriteUserDisconnect(const std::string & login,
1034 const STG::DirTraff & monthUp,
1035 const STG::DirTraff & monthDown,
1036 const STG::DirTraff & sessionUp,
1037 const STG::DirTraff & sessionDown,
1040 const std::string & reason) const
1042 std::ostringstream logStr;
1043 logStr << "Disconnect, "
1044 << " session upload: \'"
1046 << "\' session download: \'"
1048 << "\' month upload: \'"
1050 << "\' month download: \'"
1056 if (WriteLogString(logStr.str(), login))
1059 logStr << " freeMb: \'"
1066 return WriteLog2String(logStr.str(), login);
1068 //-----------------------------------------------------------------------------
1069 int FILES_STORE::SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string & login) const
1073 strprintf(&stat1,"%s/%s/stat.%d.%02d",
1074 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1076 CONFIGFILE s(stat1, true);
1080 STG_LOCKER lock(&mutex);
1081 errorStr = "Cannot create file '" + stat1 + "'";
1082 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1088 strprintf(&stat2,"%s/%s/stat2.%d.%02d",
1089 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1091 CONFIGFILE s2(stat2, true);
1095 STG_LOCKER lock(&mutex);
1096 errorStr = "Cannot create file '" + stat2 + "'";
1097 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1101 for (size_t i = 0; i < DIR_NUM; i++)
1104 snprintf(dirName, 3, "U%llu", (unsigned long long)i);
1105 s.WriteInt(dirName, stat.monthUp[i]); // Classic
1106 s2.WriteInt(dirName, stat.monthUp[i]); // New
1107 snprintf(dirName, 3, "D%llu", (unsigned long long)i);
1108 s.WriteInt(dirName, stat.monthDown[i]); // Classic
1109 s2.WriteInt(dirName, stat.monthDown[i]); // New
1113 s.WriteDouble("cash", stat.cash);
1116 s2.WriteDouble("Cash", stat.cash);
1117 s2.WriteDouble("FreeMb", stat.freeMb);
1118 s2.WriteDouble("LastCashAdd", stat.lastCashAdd);
1119 s2.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1120 s2.WriteInt("PassiveTime", stat.passiveTime);
1121 s2.WriteInt("LastActivityTime", stat.lastActivityTime);
1125 //-----------------------------------------------------------------------------*/
1126 int FILES_STORE::AddAdmin(const std::string & login) const
1128 std::string fileName;
1129 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1131 if (Touch(fileName))
1133 STG_LOCKER lock(&mutex);
1134 errorStr = "Cannot create file " + fileName;
1135 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1141 //-----------------------------------------------------------------------------*/
1142 int FILES_STORE::DelAdmin(const std::string & login) const
1144 std::string fileName;
1145 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1146 if (unlink(fileName.c_str()))
1148 STG_LOCKER lock(&mutex);
1149 errorStr = "unlink failed. Message: '";
1150 errorStr += strerror(errno);
1152 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1156 //-----------------------------------------------------------------------------*/
1157 int FILES_STORE::SaveAdmin(const STG::AdminConf & ac) const
1159 std::string fileName;
1161 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1164 CONFIGFILE cf(fileName, true);
1170 STG_LOCKER lock(&mutex);
1171 errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1172 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1176 char pass[ADM_PASSWD_LEN + 1];
1177 memset(pass, 0, sizeof(pass));
1179 char adminPass[ADM_PASSWD_LEN + 1];
1180 memset(adminPass, 0, sizeof(adminPass));
1183 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1185 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1186 adminPass[ADM_PASSWD_LEN - 1] = 0;
1188 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1190 EncryptBlock(pass + 8*i, adminPass + 8*i, &ctx);
1193 pass[ADM_PASSWD_LEN - 1] = 0;
1194 char passwordE[2 * ADM_PASSWD_LEN + 2];
1195 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1197 cf.WriteString("password", passwordE);
1198 cf.WriteInt("ChgConf", ac.priv.userConf);
1199 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1200 cf.WriteInt("ChgStat", ac.priv.userStat);
1201 cf.WriteInt("ChgCash", ac.priv.userCash);
1202 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1203 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1204 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1205 cf.WriteInt("ChgService", ac.priv.serviceChg);
1206 cf.WriteInt("ChgCorp", ac.priv.corpChg);
1211 //-----------------------------------------------------------------------------
1212 int FILES_STORE::RestoreAdmin(STG::AdminConf * ac, const std::string & login) const
1214 std::string fileName;
1215 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1216 CONFIGFILE cf(fileName);
1217 char pass[ADM_PASSWD_LEN + 1];
1218 char password[ADM_PASSWD_LEN + 1];
1219 char passwordE[2 * ADM_PASSWD_LEN + 2];
1226 STG_LOCKER lock(&mutex);
1227 errorStr = "Cannot open " + fileName;
1228 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1232 if (cf.ReadString("password", &p, "*"))
1234 STG_LOCKER lock(&mutex);
1235 errorStr = "Error in parameter password";
1236 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1240 memset(passwordE, 0, sizeof(passwordE));
1241 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1243 memset(pass, 0, sizeof(pass));
1245 if (passwordE[0] != 0)
1247 Decode21(pass, passwordE);
1248 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1250 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1252 DecryptBlock(password + 8*i, pass + 8*i, &ctx);
1260 ac->password = password;
1264 if (cf.ReadUShortInt("ChgConf", &a, 0) == 0)
1265 ac->priv.userConf = a;
1268 STG_LOCKER lock(&mutex);
1269 errorStr = "Error in parameter ChgConf";
1270 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1274 if (cf.ReadUShortInt("ChgPassword", &a, 0) == 0)
1275 ac->priv.userPasswd = a;
1278 STG_LOCKER lock(&mutex);
1279 errorStr = "Error in parameter ChgPassword";
1280 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1284 if (cf.ReadUShortInt("ChgStat", &a, 0) == 0)
1285 ac->priv.userStat = a;
1288 STG_LOCKER lock(&mutex);
1289 errorStr = "Error in parameter ChgStat";
1290 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1294 if (cf.ReadUShortInt("ChgCash", &a, 0) == 0)
1295 ac->priv.userCash = a;
1298 STG_LOCKER lock(&mutex);
1299 errorStr = "Error in parameter ChgCash";
1300 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1304 if (cf.ReadUShortInt("UsrAddDel", &a, 0) == 0)
1305 ac->priv.userAddDel = a;
1308 STG_LOCKER lock(&mutex);
1309 errorStr = "Error in parameter UsrAddDel";
1310 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1314 if (cf.ReadUShortInt("ChgAdmin", &a, 0) == 0)
1315 ac->priv.adminChg = a;
1318 STG_LOCKER lock(&mutex);
1319 errorStr = "Error in parameter ChgAdmin";
1320 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1324 if (cf.ReadUShortInt("ChgTariff", &a, 0) == 0)
1325 ac->priv.tariffChg = a;
1328 STG_LOCKER lock(&mutex);
1329 errorStr = "Error in parameter ChgTariff";
1330 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1334 if (cf.ReadUShortInt("ChgService", &a, 0) == 0)
1335 ac->priv.serviceChg = a;
1337 ac->priv.serviceChg = 0;
1339 if (cf.ReadUShortInt("ChgCorp", &a, 0) == 0)
1340 ac->priv.corpChg = a;
1342 ac->priv.corpChg = 0;
1346 //-----------------------------------------------------------------------------
1347 int FILES_STORE::AddTariff(const std::string & name) const
1349 std::string fileName;
1350 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1351 if (Touch(fileName))
1353 STG_LOCKER lock(&mutex);
1354 errorStr = "Cannot create file " + fileName;
1355 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1360 //-----------------------------------------------------------------------------
1361 int FILES_STORE::DelTariff(const std::string & name) const
1363 std::string fileName;
1364 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1365 if (unlink(fileName.c_str()))
1367 STG_LOCKER lock(&mutex);
1368 errorStr = "unlink failed. Message: '";
1369 errorStr += strerror(errno);
1371 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1375 //-----------------------------------------------------------------------------
1376 int FILES_STORE::RestoreTariff(STG::TariffData * td, const std::string & tariffName) const
1378 std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1379 CONFIGFILE conf(fileName);
1381 td->tariffConf.name = tariffName;
1383 if (conf.Error() != 0)
1385 STG_LOCKER lock(&mutex);
1386 errorStr = "Cannot read file " + fileName;
1387 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1392 for (int i = 0; i<DIR_NUM; i++)
1394 strprintf(¶m, "Time%d", i);
1395 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1397 STG_LOCKER lock(&mutex);
1398 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1399 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1403 ParseTariffTimeStr(str.c_str(),
1404 td->dirPrice[i].hDay,
1405 td->dirPrice[i].mDay,
1406 td->dirPrice[i].hNight,
1407 td->dirPrice[i].mNight);
1409 strprintf(¶m, "PriceDayA%d", i);
1410 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1412 STG_LOCKER lock(&mutex);
1413 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1414 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1417 td->dirPrice[i].priceDayA /= (1024*1024);
1419 strprintf(¶m, "PriceDayB%d", i);
1420 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1422 STG_LOCKER lock(&mutex);
1423 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1424 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1427 td->dirPrice[i].priceDayB /= (1024*1024);
1429 strprintf(¶m, "PriceNightA%d", i);
1430 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1432 STG_LOCKER lock(&mutex);
1433 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1434 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1437 td->dirPrice[i].priceNightA /= (1024*1024);
1439 strprintf(¶m, "PriceNightB%d", i);
1440 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1442 STG_LOCKER lock(&mutex);
1443 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1444 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1447 td->dirPrice[i].priceNightB /= (1024*1024);
1449 strprintf(¶m, "Threshold%d", i);
1450 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1452 STG_LOCKER lock(&mutex);
1453 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1454 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1458 strprintf(¶m, "SinglePrice%d", i);
1459 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1461 STG_LOCKER lock(&mutex);
1462 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1463 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1467 strprintf(¶m, "NoDiscount%d", i);
1468 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1470 STG_LOCKER lock(&mutex);
1471 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1472 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1477 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1479 STG_LOCKER lock(&mutex);
1480 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1481 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1485 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1487 STG_LOCKER lock(&mutex);
1488 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1489 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1493 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1495 STG_LOCKER lock(&mutex);
1496 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1497 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1501 if (conf.ReadString("TraffType", &str, "") < 0)
1503 STG_LOCKER lock(&mutex);
1504 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1505 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1509 td->tariffConf.traffType = STG::Tariff::parseTraffType(str);
1511 if (conf.ReadString("Period", &str, "month") < 0)
1512 td->tariffConf.period = STG::Tariff::MONTH;
1514 td->tariffConf.period = STG::Tariff::parsePeriod(str);
1516 if (conf.ReadString("ChangePolicy", &str, "allow") < 0)
1517 td->tariffConf.changePolicy = STG::Tariff::ALLOW;
1519 td->tariffConf.changePolicy = STG::Tariff::parseChangePolicy(str);
1521 conf.ReadTime("ChangePolicyTimeout", &td->tariffConf.changePolicyTimeout, 0);
1524 //-----------------------------------------------------------------------------
1525 int FILES_STORE::SaveTariff(const STG::TariffData & td, const std::string & tariffName) const
1527 std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1530 CONFIGFILE cf(fileName, true);
1536 STG_LOCKER lock(&mutex);
1537 errorStr = "Error writing tariff " + tariffName;
1538 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1543 for (int i = 0; i < DIR_NUM; i++)
1545 strprintf(¶m, "PriceDayA%d", i);
1546 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1548 strprintf(¶m, "PriceDayB%d", i);
1549 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1551 strprintf(¶m, "PriceNightA%d", i);
1552 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1554 strprintf(¶m, "PriceNightB%d", i);
1555 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1557 strprintf(¶m, "Threshold%d", i);
1558 cf.WriteInt(param, td.dirPrice[i].threshold);
1561 strprintf(¶m, "Time%d", i);
1563 strprintf(&s, "%0d:%0d-%0d:%0d",
1564 td.dirPrice[i].hDay,
1565 td.dirPrice[i].mDay,
1566 td.dirPrice[i].hNight,
1567 td.dirPrice[i].mNight);
1569 cf.WriteString(param, s);
1571 strprintf(¶m, "NoDiscount%d", i);
1572 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1574 strprintf(¶m, "SinglePrice%d", i);
1575 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1578 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1579 cf.WriteDouble("Fee", td.tariffConf.fee);
1580 cf.WriteDouble("Free", td.tariffConf.free);
1581 cf.WriteString("TraffType", STG::Tariff::toString(td.tariffConf.traffType));
1582 cf.WriteString("Period", STG::Tariff::toString(td.tariffConf.period));
1583 cf.WriteString("ChangePolicy", STG::Tariff::toString(td.tariffConf.changePolicy));
1584 cf.WriteTime("ChangePolicyTimeout", td.tariffConf.changePolicyTimeout);
1589 //-----------------------------------------------------------------------------*/
1590 int FILES_STORE::AddService(const std::string & name) const
1592 std::string fileName;
1593 strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str());
1595 if (Touch(fileName))
1597 STG_LOCKER lock(&mutex);
1598 errorStr = "Cannot create file " + fileName;
1599 printfd(__FILE__, "FILES_STORE::AddService - failed to add service '%s'\n", name.c_str());
1605 //-----------------------------------------------------------------------------*/
1606 int FILES_STORE::DelService(const std::string & name) const
1608 std::string fileName;
1609 strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str());
1610 if (unlink(fileName.c_str()))
1612 STG_LOCKER lock(&mutex);
1613 errorStr = "unlink failed. Message: '";
1614 errorStr += strerror(errno);
1616 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1620 //-----------------------------------------------------------------------------*/
1621 int FILES_STORE::SaveService(const STG::ServiceConf & conf) const
1623 std::string fileName;
1625 strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), conf.name.c_str());
1628 CONFIGFILE cf(fileName, true);
1634 STG_LOCKER lock(&mutex);
1635 errorStr = "Cannot write service " + conf.name + ". " + fileName;
1636 printfd(__FILE__, "FILES_STORE::SaveService - failed to save service '%s'\n", conf.name.c_str());
1640 cf.WriteString("name", conf.name);
1641 cf.WriteString("comment", conf.comment);
1642 cf.WriteDouble("cost", conf.cost);
1643 cf.WriteInt("pay_day", conf.payDay);
1648 //-----------------------------------------------------------------------------
1649 int FILES_STORE::RestoreService(STG::ServiceConf * conf, const std::string & name) const
1651 std::string fileName;
1652 strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str());
1653 CONFIGFILE cf(fileName);
1657 STG_LOCKER lock(&mutex);
1658 errorStr = "Cannot open " + fileName;
1659 printfd(__FILE__, "FILES_STORE::RestoreService - failed to restore service '%s'\n", name.c_str());
1663 if (cf.ReadString("name", &conf->name, name))
1665 STG_LOCKER lock(&mutex);
1666 errorStr = "Error in parameter 'name'";
1667 printfd(__FILE__, "FILES_STORE::RestoreService - name read failed for service '%s'\n", name.c_str());
1671 if (cf.ReadString("comment", &conf->comment, ""))
1673 STG_LOCKER lock(&mutex);
1674 errorStr = "Error in parameter 'comment'";
1675 printfd(__FILE__, "FILES_STORE::RestoreService - comment read failed for service '%s'\n", name.c_str());
1679 if (cf.ReadDouble("cost", &conf->cost, 0.0))
1681 STG_LOCKER lock(&mutex);
1682 errorStr = "Error in parameter 'cost'";
1683 printfd(__FILE__, "FILES_STORE::RestoreService - cost read failed for service '%s'\n", name.c_str());
1687 unsigned short value = 0;
1688 if (cf.ReadUShortInt("pay_day", &value, 0))
1690 STG_LOCKER lock(&mutex);
1691 errorStr = "Error in parameter 'pay_day'";
1692 printfd(__FILE__, "FILES_STORE::RestoreService - pay day read failed for service '%s'\n", name.c_str());
1695 conf->payDay = value;
1699 //-----------------------------------------------------------------------------
1700 int FILES_STORE::WriteDetailedStat(const STG::TraffStat & statTree,
1702 const std::string & login) const
1704 char fn[FN_STR_LEN];
1705 char dn[FN_STR_LEN];
1712 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1713 if (access(dn, F_OK) != 0)
1715 if (mkdir(dn, 0700) != 0)
1717 STG_LOCKER lock(&mutex);
1718 errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1719 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1724 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1725 e += chmod(dn, storeSettings.GetStatModeDir());
1729 STG_LOCKER lock(&mutex);
1730 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1735 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1741 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1742 storeSettings.GetUsersDir().c_str(),
1746 if (access(dn, F_OK) != 0)
1748 if (mkdir(dn, 0700) != 0)
1750 STG_LOCKER lock(&mutex);
1751 errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1752 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1757 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1758 e += chmod(dn, storeSettings.GetStatModeDir());
1762 STG_LOCKER lock(&mutex);
1763 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1766 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1767 storeSettings.GetUsersDir().c_str(),
1770 lt->tm_mon+1 < 10 ? "0" : "",
1772 if (access(dn, F_OK) != 0)
1774 if (mkdir(dn, 0700) != 0)
1776 STG_LOCKER lock(&mutex);
1777 errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1778 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1783 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1784 e += chmod(dn, storeSettings.GetStatModeDir());
1788 STG_LOCKER lock(&mutex);
1789 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1792 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1794 statFile = fopen (fn, "at");
1798 STG_LOCKER lock(&mutex);
1799 errorStr = "File \'" + std::string(fn) + "\' cannot be written.";
1800 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1807 lt1 = localtime(&lastStat);
1816 lt2 = localtime(&t);
1822 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1823 h1, m1, s1, h2, m2, s2) < 0)
1825 STG_LOCKER lock(&mutex);
1826 errorStr = std::string("fprint failed. Message: '") + strerror(errno) + "'";
1827 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1832 auto stIter = statTree.begin();
1834 while (stIter != statTree.end())
1836 const auto u = std::to_string(stIter->second.up);
1837 const auto d = std::to_string(stIter->second.down);
1838 #ifdef TRAFF_STAT_WITH_PORTS
1839 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1840 inet_ntostring(stIter->first.ip).c_str(),
1845 stIter->second.cash) < 0)
1847 STG_LOCKER lock(&mutex);
1848 errorStr = "fprint failed. Message: '";
1849 errorStr += strerror(errno);
1851 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1856 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1857 inet_ntostring(stIter->first.ip).c_str(),
1861 stIter->second.cash) < 0)
1863 STG_LOCKER lock(&mutex);
1864 errorStr = std::string("fprint failed. Message: '");
1865 errorStr += strerror(errno);
1867 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1878 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1879 e += chmod(fn, storeSettings.GetStatMode());
1883 STG_LOCKER lock(&mutex);
1884 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1889 //-----------------------------------------------------------------------------
1890 int FILES_STORE::AddMessage(STG::Message * msg, const std::string & login) const
1896 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1897 if (access(dn.c_str(), F_OK) != 0)
1899 if (mkdir(dn.c_str(), 0700) != 0)
1901 STG_LOCKER lock(&mutex);
1902 errorStr = "Directory \'";
1904 errorStr += "\' cannot be created.";
1905 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1910 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1912 gettimeofday(&tv, NULL);
1914 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1915 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1919 STG_LOCKER lock(&mutex);
1920 errorStr = "File \'";
1922 errorStr += "\' cannot be writen.";
1923 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1927 return EditMessage(*msg, login);
1929 //-----------------------------------------------------------------------------
1930 int FILES_STORE::EditMessage(const STG::Message & msg, const std::string & login) const
1932 std::string fileName;
1935 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1937 if (access(fileName.c_str(), F_OK) != 0)
1939 STG_LOCKER lock(&mutex);
1940 errorStr = "Message for user \'";
1941 errorStr += login + "\' with ID \'";
1942 errorStr += std::to_string(msg.header.id) + "\' does not exist.";
1943 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
1947 Touch(fileName + ".new");
1949 msgFile = fopen((fileName + ".new").c_str(), "wt");
1952 STG_LOCKER lock(&mutex);
1953 errorStr = "File \'" + fileName + "\' cannot be writen.";
1954 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1959 res &= (fprintf(msgFile, "%u\n", msg.header.type) >= 0);
1960 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1961 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1962 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1963 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1964 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1965 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1969 STG_LOCKER lock(&mutex);
1970 errorStr = std::string("fprintf failed. Message: '") + strerror(errno) + "'";
1971 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
1978 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
1980 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1982 STG_LOCKER lock(&mutex);
1983 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1984 printfd(__FILE__, "FILES_STORE::EditMessage - rename failed. Message: '%s'\n", strerror(errno));
1990 //-----------------------------------------------------------------------------
1991 int FILES_STORE::GetMessage(uint64_t id, STG::Message * msg, const std::string & login) const
1994 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1995 msg->header.id = id;
1996 return ReadMessage(fn, &msg->header, &msg->text);
1998 //-----------------------------------------------------------------------------
1999 int FILES_STORE::DelMessage(uint64_t id, const std::string & login) const
2002 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2004 return unlink(fn.c_str());
2006 //-----------------------------------------------------------------------------
2007 int FILES_STORE::GetMessageHdrs(std::vector<STG::Message::Header> * hdrsList, const std::string & login) const
2009 std::string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/");
2011 if (access(dn.c_str(), F_OK) != 0)
2016 std::vector<std::string> messages;
2017 GetFileList(&messages, dn, S_IFREG, "");
2019 for (unsigned i = 0; i < messages.size(); i++)
2021 unsigned long long id = 0;
2023 if (str2x(messages[i].c_str(), id))
2025 if (unlink((dn + messages[i]).c_str()))
2027 STG_LOCKER lock(&mutex);
2028 errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'";
2029 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2035 STG::Message::Header hdr;
2036 if (ReadMessage(dn + messages[i], &hdr, NULL))
2043 if (unlink((dn + messages[i]).c_str()))
2045 STG_LOCKER lock(&mutex);
2046 errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'";
2047 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2054 hdrsList->push_back(hdr);
2058 //-----------------------------------------------------------------------------
2059 int FILES_STORE::ReadMessage(const std::string & fileName,
2060 STG::Message::Header * hdr,
2061 std::string * text) const
2064 msgFile = fopen(fileName.c_str(), "rt");
2067 STG_LOCKER lock(&mutex);
2068 errorStr = "File \'";
2069 errorStr += fileName;
2070 errorStr += "\' cannot be openned.";
2071 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
2077 d[1] = &hdr->lastSendTime;
2078 d[2] = &hdr->creationTime;
2079 d[3] = &hdr->showTime;
2080 d[4] = (unsigned*)(&hdr->repeat);
2081 d[5] = &hdr->repeatPeriod;
2083 memset(p, 0, sizeof(p));
2085 for (int pos = 0; pos < 6; pos++)
2087 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
2088 STG_LOCKER lock(&mutex);
2089 errorStr = "Cannot read file \'";
2090 errorStr += fileName;
2091 errorStr += "\'. Missing data.";
2092 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
2093 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2099 ep = strrchr(p, '\r');
2101 ep = strrchr(p, '\n');
2106 STG_LOCKER lock(&mutex);
2107 errorStr = "Cannot read file \'";
2108 errorStr += fileName;
2109 errorStr += "\'. Missing data.";
2110 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
2111 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2116 if (str2x(p, *(d[pos])))
2118 STG_LOCKER lock(&mutex);
2119 errorStr = "Cannot read file \'";
2120 errorStr += fileName;
2121 errorStr += "\'. Incorrect value. \'";
2124 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
2131 memset(txt, 0, sizeof(txt));
2134 text->erase(text->begin(), text->end());
2135 while (!feof(msgFile))
2138 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2148 //-----------------------------------------------------------------------------
2149 int FILES_STORE::Touch(const std::string & path) const
2151 FILE * f = fopen(path.c_str(), "wb");
2159 //-----------------------------------------------------------------------------
2160 int GetFileList(std::vector<std::string> * fileList, const std::string & directory, mode_t mode, const std::string & ext)
2162 DIR * d = opendir(directory.c_str());
2166 printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno));
2171 while ((entry = readdir(d)))
2173 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
2176 std::string str = directory + "/" + std::string(entry->d_name);
2179 if (stat(str.c_str(), &st))
2182 if (!(st.st_mode & mode)) // Filter by mode
2188 size_t d_nameLen = strlen(entry->d_name);
2189 if (d_nameLen <= ext.size())
2192 if (ext == entry->d_name + (d_nameLen - ext.size()))
2194 entry->d_name[d_nameLen - ext.size()] = 0;
2195 fileList->push_back(entry->d_name);
2200 fileList->push_back(entry->d_name);
2208 //-----------------------------------------------------------------------------