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 $
46 #include "stg/common.h"
47 #include "stg/user_ips.h"
48 #include "stg/user_conf.h"
49 #include "stg/user_stat.h"
50 #include "stg/const.h"
51 #include "stg/blowfish.h"
52 #include "stg/logger.h"
53 #include "stg/locker.h"
54 #include "stg/plugin_creator.h"
55 #include "file_store.h"
57 #define DELETED_USERS_DIR "deleted_users"
59 #define adm_enc_passwd "cjeifY8m3"
61 int GetFileList(std::vector<std::string> * fileList, const std::string & directory, mode_t mode, const std::string & ext);
63 const int pt_mega = 1024 * 1024;
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
69 PLUGIN_CREATOR<FILES_STORE> fsc;
71 bool CheckAndCreate(const std::string & dir, mode_t mode)
73 if (access(dir.c_str(), F_OK) == 0)
75 if (mkdir(dir.c_str(), mode) == 0)
82 extern "C" STORE * GetStore();
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
88 return fsc.GetPlugin();
90 //-----------------------------------------------------------------------------
91 FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
106 //-----------------------------------------------------------------------------
107 int FILES_STORE_SETTINGS::ParseOwner(const std::vector<PARAM_VALUE> & moduleParams, const std::string & owner, uid_t * uid)
111 std::vector<PARAM_VALUE>::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<PARAM_VALUE> & moduleParams, const std::string & group, gid_t * gid)
132 std::vector<PARAM_VALUE>::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<PARAM_VALUE> & moduleParams, const std::string & modeStr, mode_t * mode)
170 std::vector<PARAM_VALUE>::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 MODULE_SETTINGS & 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<PARAM_VALUE>::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()
374 version("file_store v.1.04"),
378 logger(GetPluginLogger(GetStgLogger(), "store_files"))
380 pthread_mutexattr_t attr;
381 pthread_mutexattr_init(&attr);
382 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
383 pthread_mutex_init(&mutex, &attr);
385 //-----------------------------------------------------------------------------
386 int FILES_STORE::ParseSettings()
388 int ret = storeSettings.ParseSettings(settings);
391 STG_LOCKER lock(&mutex);
392 errorStr = storeSettings.GetStrError();
396 //-----------------------------------------------------------------------------
397 int FILES_STORE::GetUsersList(std::vector<std::string> * userList) const
399 std::vector<std::string> files;
401 if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, ""))
403 STG_LOCKER lock(&mutex);
404 errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + std::string(strerror(errno));
408 STG_LOCKER lock(&mutex);
410 userList->swap(files);
414 //-----------------------------------------------------------------------------
415 int FILES_STORE::GetAdminsList(std::vector<std::string> * adminList) const
417 std::vector<std::string> files;
419 if (GetFileList(&files, storeSettings.GetAdminsDir(), S_IFREG, ".adm"))
421 STG_LOCKER lock(&mutex);
422 errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + std::string(strerror(errno));
426 STG_LOCKER lock(&mutex);
428 adminList->swap(files);
432 //-----------------------------------------------------------------------------
433 int FILES_STORE::GetTariffsList(std::vector<std::string> * tariffList) const
435 std::vector<std::string> files;
437 if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf"))
439 STG_LOCKER lock(&mutex);
440 errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + std::string(strerror(errno));
444 STG_LOCKER lock(&mutex);
446 tariffList->swap(files);
450 //-----------------------------------------------------------------------------
451 int FILES_STORE::GetServicesList(std::vector<std::string> * list) const
453 std::vector<std::string> files;
455 if (GetFileList(&files, storeSettings.GetServicesDir(), S_IFREG, ".serv"))
457 STG_LOCKER lock(&mutex);
458 errorStr = "Failed to open '" + storeSettings.GetServicesDir() + "': " + std::string(strerror(errno));
462 STG_LOCKER lock(&mutex);
468 //-----------------------------------------------------------------------------
469 int FILES_STORE::RemoveDir(const char * path) const
471 DIR * d = opendir(path);
475 errorStr = "failed to open dir. Message: '";
476 errorStr += strerror(errno);
478 printfd(__FILE__, "FILE_STORE::RemoveDir() - Failed to open dir '%s': '%s'\n", path, strerror(errno));
483 while ((entry = readdir(d)))
485 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
488 std::string str = path;
489 str += "/" + std::string(entry->d_name);
492 if (stat(str.c_str(), &st))
495 if ((st.st_mode & S_IFREG))
497 if (unlink(str.c_str()))
499 STG_LOCKER lock(&mutex);
500 errorStr = "unlink failed. Message: '";
501 errorStr += strerror(errno);
503 printfd(__FILE__, "FILES_STORE::RemoveDir() - unlink failed. Message: '%s'\n", strerror(errno));
509 if (!(st.st_mode & S_IFDIR))
511 if (RemoveDir(str.c_str()))
524 STG_LOCKER lock(&mutex);
525 errorStr = "rmdir failed. Message: '";
526 errorStr += strerror(errno);
528 printfd(__FILE__, "FILES_STORE::RemoveDir() - rmdir failed. Message: '%s'\n", strerror(errno));
534 //-----------------------------------------------------------------------------
535 int FILES_STORE::AddUser(const std::string & login) const
537 std::string fileName;
539 strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str());
541 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
543 STG_LOCKER lock(&mutex);
544 errorStr = std::string("mkdir failed. Message: '") + strerror(errno) + "'";
545 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
549 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
552 STG_LOCKER lock(&mutex);
553 errorStr = "Cannot create file \"" + fileName + "\'";
554 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
558 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
561 STG_LOCKER lock(&mutex);
562 errorStr = "Cannot create file \"" + fileName + "\'";
563 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
568 //-----------------------------------------------------------------------------
569 int FILES_STORE::DelUser(const std::string & login) const
572 std::string dirName1;
574 strprintf(&dirName, "%s/%s", storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR);
575 if (access(dirName.c_str(), F_OK) != 0)
577 if (mkdir(dirName.c_str(), 0700) != 0)
579 STG_LOCKER lock(&mutex);
580 errorStr = "Directory '" + dirName + "' cannot be created.";
581 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
586 if (access(dirName.c_str(), F_OK) == 0)
588 strprintf(&dirName, "%s/%s/%s.%lu", storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR, login.c_str(), time(NULL));
589 strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
590 if (rename(dirName1.c_str(), dirName.c_str()))
592 STG_LOCKER lock(&mutex);
593 errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
594 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
600 strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
601 if (RemoveDir(dirName.c_str()))
608 //-----------------------------------------------------------------------------
609 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const
611 std::string fileName;
612 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
613 if (RestoreUserConf(conf, login, fileName))
615 if (!storeSettings.GetReadBak())
619 return RestoreUserConf(conf, login, fileName + ".bak");
623 //-----------------------------------------------------------------------------
624 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login, const std::string & fileName) const
626 CONFIGFILE cf(fileName);
631 STG_LOCKER lock(&mutex);
632 errorStr = "User \'" + login + "\' data not read.";
633 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
637 if (cf.ReadString("Password", &conf->password, "") < 0)
639 STG_LOCKER lock(&mutex);
640 errorStr = "User \'" + login + "\' data not read. Parameter Password.";
641 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
644 if (conf->password.empty())
646 STG_LOCKER lock(&mutex);
647 errorStr = "User \'" + login + "\' password is blank.";
648 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
652 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
654 STG_LOCKER lock(&mutex);
655 errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
656 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
659 if (conf->tariffName.empty())
661 STG_LOCKER lock(&mutex);
662 errorStr = "User \'" + login + "\' tariff is blank.";
663 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
668 cf.ReadString("IP", &ipStr, "?");
672 ips = StrToIPS(ipStr);
674 catch (const std::string & s)
676 STG_LOCKER lock(&mutex);
677 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
678 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
683 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
685 STG_LOCKER lock(&mutex);
686 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
687 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
691 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
693 STG_LOCKER lock(&mutex);
694 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
695 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
699 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
701 STG_LOCKER lock(&mutex);
702 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
703 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
707 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
708 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
709 cf.ReadString("TariffChange", &conf->nextTariff, "");
710 cf.ReadString("Group", &conf->group, "");
711 cf.ReadString("RealName", &conf->realName, "");
712 cf.ReadString("Address", &conf->address, "");
713 cf.ReadString("Phone", &conf->phone, "");
714 cf.ReadString("Note", &conf->note, "");
715 cf.ReadString("email", &conf->email, "");
717 char userdataName[12];
718 for (int i = 0; i < USERDATA_NUM; i++)
720 snprintf(userdataName, 12, "Userdata%d", i);
721 cf.ReadString(userdataName, &conf->userdata[i], "");
724 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
726 STG_LOCKER lock(&mutex);
727 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
728 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
734 //-----------------------------------------------------------------------------
735 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const
737 std::string fileName;
738 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
740 if (RestoreUserStat(stat, login, fileName))
742 if (!storeSettings.GetReadBak())
746 return RestoreUserStat(stat, login, fileName + ".bak");
750 //-----------------------------------------------------------------------------
751 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login, const std::string & fileName) const
753 CONFIGFILE cf(fileName);
759 STG_LOCKER lock(&mutex);
760 errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
761 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
767 for (int i = 0; i < DIR_NUM; i++)
770 snprintf(s, 22, "D%d", i);
771 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
773 STG_LOCKER lock(&mutex);
774 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
775 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
778 stat->monthDown[i] = traff;
780 snprintf(s, 22, "U%d", i);
781 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
783 STG_LOCKER lock(&mutex);
784 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
785 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
788 stat->monthUp[i] = traff;
791 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
793 STG_LOCKER lock(&mutex);
794 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
795 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
799 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
801 STG_LOCKER lock(&mutex);
802 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
803 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
807 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
809 STG_LOCKER lock(&mutex);
810 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
811 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
815 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
817 STG_LOCKER lock(&mutex);
818 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
819 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
823 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
825 STG_LOCKER lock(&mutex);
826 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
827 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
831 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
833 STG_LOCKER lock(&mutex);
834 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
835 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
841 //-----------------------------------------------------------------------------
842 int FILES_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const
844 std::string fileName;
845 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
847 CONFIGFILE cfstat(fileName, true);
849 int e = cfstat.Error();
853 STG_LOCKER lock(&mutex);
854 errorStr = std::string("User \'") + login + "\' conf not written\n";
855 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
859 e = chmod(fileName.c_str(), storeSettings.GetConfMode());
860 e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID());
864 STG_LOCKER lock(&mutex);
865 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
868 cfstat.WriteString("Password", conf.password);
869 cfstat.WriteInt ("Passive", conf.passive);
870 cfstat.WriteInt ("Down", conf.disabled);
871 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
872 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
873 cfstat.WriteString("Tariff", conf.tariffName);
874 cfstat.WriteString("Address", conf.address);
875 cfstat.WriteString("Phone", conf.phone);
876 cfstat.WriteString("Email", conf.email);
877 cfstat.WriteString("Note", conf.note);
878 cfstat.WriteString("RealName", conf.realName);
879 cfstat.WriteString("Group", conf.group);
880 cfstat.WriteDouble("Credit", conf.credit);
881 cfstat.WriteString("TariffChange", conf.nextTariff);
883 char userdataName[12];
884 for (int i = 0; i < USERDATA_NUM; i++)
886 snprintf(userdataName, 12, "Userdata%d", i);
887 cfstat.WriteString(userdataName, conf.userdata[i]);
889 cfstat.WriteInt("CreditExpire", conf.creditExpire);
891 std::ostringstream ipStr;
893 cfstat.WriteString("IP", ipStr.str());
897 //-----------------------------------------------------------------------------
898 int FILES_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
900 std::string fileName;
901 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
904 CONFIGFILE cfstat(fileName, true);
905 int e = cfstat.Error();
909 STG_LOCKER lock(&mutex);
910 errorStr = std::string("User \'") + login + "\' stat not written\n";
911 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
915 for (int i = 0; i < DIR_NUM; i++)
918 snprintf(s, 22, "D%d", i);
919 cfstat.WriteInt(s, stat.monthDown[i]);
920 snprintf(s, 22, "U%d", i);
921 cfstat.WriteInt(s, stat.monthUp[i]);
924 cfstat.WriteDouble("Cash", stat.cash);
925 cfstat.WriteDouble("FreeMb", stat.freeMb);
926 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
927 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
928 cfstat.WriteInt("PassiveTime", stat.passiveTime);
929 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
932 int e = chmod(fileName.c_str(), storeSettings.GetStatMode());
933 e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID());
937 STG_LOCKER lock(&mutex);
938 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
943 //-----------------------------------------------------------------------------
944 int FILES_STORE::WriteLogString(const std::string & str, const std::string & login) const
947 time_t tm = time(NULL);
948 std::string fileName;
949 fileName = storeSettings.GetUsersDir() + "/" + login + "/log";
950 f = fopen(fileName.c_str(), "at");
954 fprintf(f, "%s", LogDate(tm));
956 fprintf(f, "%s", str.c_str());
962 STG_LOCKER lock(&mutex);
963 errorStr = "Cannot open \'" + fileName + "\'";
964 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
968 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
969 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
973 STG_LOCKER lock(&mutex);
974 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
979 //-----------------------------------------------------------------------------
980 int FILES_STORE::WriteLog2String(const std::string & str, const std::string & login) const
983 time_t tm = time(NULL);
984 std::string fileName;
985 fileName = storeSettings.GetUsersDir() + "/" + login + "/log2";
986 f = fopen(fileName.c_str(), "at");
990 fprintf(f, "%s", LogDate(tm));
992 fprintf(f, "%s", str.c_str());
998 STG_LOCKER lock(&mutex);
999 errorStr = "Cannot open \'" + fileName + "\'";
1000 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1004 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1005 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1009 STG_LOCKER lock(&mutex);
1010 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1015 //-----------------------------------------------------------------------------
1016 int FILES_STORE::WriteUserChgLog(const std::string & login,
1017 const std::string & admLogin,
1019 const std::string & paramName,
1020 const std::string & oldValue,
1021 const std::string & newValue,
1022 const std::string & message) const
1024 std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1025 + paramName + "\' parameter changed from \'" + oldValue +
1026 "\' to \'" + newValue + "\'. " + message;
1028 return WriteLogString(userLogMsg, login);
1030 //-----------------------------------------------------------------------------
1031 int FILES_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
1033 std::string logStr = "Connect, " + inet_ntostring(ip);
1034 if (WriteLogString(logStr, login))
1036 return WriteLog2String(logStr, login);
1038 //-----------------------------------------------------------------------------
1039 int FILES_STORE::WriteUserDisconnect(const std::string & login,
1040 const DIR_TRAFF & monthUp,
1041 const DIR_TRAFF & monthDown,
1042 const DIR_TRAFF & sessionUp,
1043 const DIR_TRAFF & sessionDown,
1046 const std::string & reason) const
1048 std::ostringstream logStr;
1049 logStr << "Disconnect, "
1050 << " session upload: \'"
1052 << "\' session download: \'"
1054 << "\' month upload: \'"
1056 << "\' month download: \'"
1062 if (WriteLogString(logStr.str(), login))
1065 logStr << " freeMb: \'"
1072 return WriteLog2String(logStr.str(), login);
1074 //-----------------------------------------------------------------------------
1075 int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const std::string & login) const
1079 strprintf(&stat1,"%s/%s/stat.%d.%02d",
1080 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1082 CONFIGFILE s(stat1, true);
1086 STG_LOCKER lock(&mutex);
1087 errorStr = "Cannot create file '" + stat1 + "'";
1088 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1094 strprintf(&stat2,"%s/%s/stat2.%d.%02d",
1095 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1097 CONFIGFILE s2(stat2, true);
1101 STG_LOCKER lock(&mutex);
1102 errorStr = "Cannot create file '" + stat2 + "'";
1103 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1107 for (size_t i = 0; i < DIR_NUM; i++)
1110 snprintf(dirName, 3, "U%llu", (unsigned long long)i);
1111 s.WriteInt(dirName, stat.monthUp[i]); // Classic
1112 s2.WriteInt(dirName, stat.monthUp[i]); // New
1113 snprintf(dirName, 3, "D%llu", (unsigned long long)i);
1114 s.WriteInt(dirName, stat.monthDown[i]); // Classic
1115 s2.WriteInt(dirName, stat.monthDown[i]); // New
1119 s.WriteDouble("cash", stat.cash);
1122 s2.WriteDouble("Cash", stat.cash);
1123 s2.WriteDouble("FreeMb", stat.freeMb);
1124 s2.WriteDouble("LastCashAdd", stat.lastCashAdd);
1125 s2.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1126 s2.WriteInt("PassiveTime", stat.passiveTime);
1127 s2.WriteInt("LastActivityTime", stat.lastActivityTime);
1131 //-----------------------------------------------------------------------------*/
1132 int FILES_STORE::AddAdmin(const std::string & login) const
1134 std::string fileName;
1135 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1137 if (Touch(fileName))
1139 STG_LOCKER lock(&mutex);
1140 errorStr = "Cannot create file " + fileName;
1141 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1147 //-----------------------------------------------------------------------------*/
1148 int FILES_STORE::DelAdmin(const std::string & login) const
1150 std::string fileName;
1151 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1152 if (unlink(fileName.c_str()))
1154 STG_LOCKER lock(&mutex);
1155 errorStr = "unlink failed. Message: '";
1156 errorStr += strerror(errno);
1158 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1162 //-----------------------------------------------------------------------------*/
1163 int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1165 std::string fileName;
1167 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1170 CONFIGFILE cf(fileName, true);
1176 STG_LOCKER lock(&mutex);
1177 errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1178 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1182 char pass[ADM_PASSWD_LEN + 1];
1183 memset(pass, 0, sizeof(pass));
1185 char adminPass[ADM_PASSWD_LEN + 1];
1186 memset(adminPass, 0, sizeof(adminPass));
1189 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1191 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1192 adminPass[ADM_PASSWD_LEN - 1] = 0;
1194 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1196 EncryptBlock(pass + 8*i, adminPass + 8*i, &ctx);
1199 pass[ADM_PASSWD_LEN - 1] = 0;
1200 char passwordE[2 * ADM_PASSWD_LEN + 2];
1201 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1203 cf.WriteString("password", passwordE);
1204 cf.WriteInt("ChgConf", ac.priv.userConf);
1205 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1206 cf.WriteInt("ChgStat", ac.priv.userStat);
1207 cf.WriteInt("ChgCash", ac.priv.userCash);
1208 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1209 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1210 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1211 cf.WriteInt("ChgService", ac.priv.serviceChg);
1212 cf.WriteInt("ChgCorp", ac.priv.corpChg);
1217 //-----------------------------------------------------------------------------
1218 int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const
1220 std::string fileName;
1221 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1222 CONFIGFILE cf(fileName);
1223 char pass[ADM_PASSWD_LEN + 1];
1224 char password[ADM_PASSWD_LEN + 1];
1225 char passwordE[2 * ADM_PASSWD_LEN + 2];
1232 STG_LOCKER lock(&mutex);
1233 errorStr = "Cannot open " + fileName;
1234 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1238 if (cf.ReadString("password", &p, "*"))
1240 STG_LOCKER lock(&mutex);
1241 errorStr = "Error in parameter password";
1242 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1246 memset(passwordE, 0, sizeof(passwordE));
1247 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1249 memset(pass, 0, sizeof(pass));
1251 if (passwordE[0] != 0)
1253 Decode21(pass, passwordE);
1254 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1256 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1258 DecryptBlock(password + 8*i, pass + 8*i, &ctx);
1266 ac->password = password;
1270 if (cf.ReadUShortInt("ChgConf", &a, 0) == 0)
1271 ac->priv.userConf = a;
1274 STG_LOCKER lock(&mutex);
1275 errorStr = "Error in parameter ChgConf";
1276 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1280 if (cf.ReadUShortInt("ChgPassword", &a, 0) == 0)
1281 ac->priv.userPasswd = a;
1284 STG_LOCKER lock(&mutex);
1285 errorStr = "Error in parameter ChgPassword";
1286 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1290 if (cf.ReadUShortInt("ChgStat", &a, 0) == 0)
1291 ac->priv.userStat = a;
1294 STG_LOCKER lock(&mutex);
1295 errorStr = "Error in parameter ChgStat";
1296 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1300 if (cf.ReadUShortInt("ChgCash", &a, 0) == 0)
1301 ac->priv.userCash = a;
1304 STG_LOCKER lock(&mutex);
1305 errorStr = "Error in parameter ChgCash";
1306 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1310 if (cf.ReadUShortInt("UsrAddDel", &a, 0) == 0)
1311 ac->priv.userAddDel = a;
1314 STG_LOCKER lock(&mutex);
1315 errorStr = "Error in parameter UsrAddDel";
1316 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1320 if (cf.ReadUShortInt("ChgAdmin", &a, 0) == 0)
1321 ac->priv.adminChg = a;
1324 STG_LOCKER lock(&mutex);
1325 errorStr = "Error in parameter ChgAdmin";
1326 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1330 if (cf.ReadUShortInt("ChgTariff", &a, 0) == 0)
1331 ac->priv.tariffChg = a;
1334 STG_LOCKER lock(&mutex);
1335 errorStr = "Error in parameter ChgTariff";
1336 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1340 if (cf.ReadUShortInt("ChgService", &a, 0) == 0)
1341 ac->priv.serviceChg = a;
1343 ac->priv.serviceChg = 0;
1345 if (cf.ReadUShortInt("ChgCorp", &a, 0) == 0)
1346 ac->priv.corpChg = a;
1348 ac->priv.corpChg = 0;
1352 //-----------------------------------------------------------------------------
1353 int FILES_STORE::AddTariff(const std::string & name) const
1355 std::string fileName;
1356 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1357 if (Touch(fileName))
1359 STG_LOCKER lock(&mutex);
1360 errorStr = "Cannot create file " + fileName;
1361 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1366 //-----------------------------------------------------------------------------
1367 int FILES_STORE::DelTariff(const std::string & name) const
1369 std::string fileName;
1370 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1371 if (unlink(fileName.c_str()))
1373 STG_LOCKER lock(&mutex);
1374 errorStr = "unlink failed. Message: '";
1375 errorStr += strerror(errno);
1377 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1381 //-----------------------------------------------------------------------------
1382 int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const
1384 std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1385 CONFIGFILE conf(fileName);
1387 td->tariffConf.name = tariffName;
1389 if (conf.Error() != 0)
1391 STG_LOCKER lock(&mutex);
1392 errorStr = "Cannot read file " + fileName;
1393 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1398 for (int i = 0; i<DIR_NUM; i++)
1400 strprintf(¶m, "Time%d", i);
1401 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1403 STG_LOCKER lock(&mutex);
1404 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1405 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1409 ParseTariffTimeStr(str.c_str(),
1410 td->dirPrice[i].hDay,
1411 td->dirPrice[i].mDay,
1412 td->dirPrice[i].hNight,
1413 td->dirPrice[i].mNight);
1415 strprintf(¶m, "PriceDayA%d", i);
1416 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1418 STG_LOCKER lock(&mutex);
1419 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1420 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1423 td->dirPrice[i].priceDayA /= (1024*1024);
1425 strprintf(¶m, "PriceDayB%d", i);
1426 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1428 STG_LOCKER lock(&mutex);
1429 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1430 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1433 td->dirPrice[i].priceDayB /= (1024*1024);
1435 strprintf(¶m, "PriceNightA%d", i);
1436 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1438 STG_LOCKER lock(&mutex);
1439 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1440 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1443 td->dirPrice[i].priceNightA /= (1024*1024);
1445 strprintf(¶m, "PriceNightB%d", i);
1446 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1448 STG_LOCKER lock(&mutex);
1449 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1450 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1453 td->dirPrice[i].priceNightB /= (1024*1024);
1455 strprintf(¶m, "Threshold%d", i);
1456 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1458 STG_LOCKER lock(&mutex);
1459 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1460 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1464 strprintf(¶m, "SinglePrice%d", i);
1465 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1467 STG_LOCKER lock(&mutex);
1468 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1469 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1473 strprintf(¶m, "NoDiscount%d", i);
1474 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1476 STG_LOCKER lock(&mutex);
1477 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1478 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1483 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1485 STG_LOCKER lock(&mutex);
1486 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1487 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1491 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1493 STG_LOCKER lock(&mutex);
1494 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1495 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1499 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1501 STG_LOCKER lock(&mutex);
1502 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1503 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1507 if (conf.ReadString("TraffType", &str, "") < 0)
1509 STG_LOCKER lock(&mutex);
1510 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1511 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1515 td->tariffConf.traffType = TARIFF::StringToTraffType(str);
1517 if (conf.ReadString("Period", &str, "month") < 0)
1518 td->tariffConf.period = TARIFF::MONTH;
1520 td->tariffConf.period = TARIFF::StringToPeriod(str);
1522 if (conf.ReadString("ChangePolicy", &str, "allow") < 0)
1523 td->tariffConf.changePolicy = TARIFF::ALLOW;
1525 td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(str);
1527 conf.ReadTime("ChangePolicyTimeout", &td->tariffConf.changePolicyTimeout, 0);
1530 //-----------------------------------------------------------------------------
1531 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const
1533 std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1536 CONFIGFILE cf(fileName, true);
1542 STG_LOCKER lock(&mutex);
1543 errorStr = "Error writing tariff " + tariffName;
1544 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1549 for (int i = 0; i < DIR_NUM; i++)
1551 strprintf(¶m, "PriceDayA%d", i);
1552 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1554 strprintf(¶m, "PriceDayB%d", i);
1555 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1557 strprintf(¶m, "PriceNightA%d", i);
1558 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1560 strprintf(¶m, "PriceNightB%d", i);
1561 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1563 strprintf(¶m, "Threshold%d", i);
1564 cf.WriteInt(param, td.dirPrice[i].threshold);
1567 strprintf(¶m, "Time%d", i);
1569 strprintf(&s, "%0d:%0d-%0d:%0d",
1570 td.dirPrice[i].hDay,
1571 td.dirPrice[i].mDay,
1572 td.dirPrice[i].hNight,
1573 td.dirPrice[i].mNight);
1575 cf.WriteString(param, s);
1577 strprintf(¶m, "NoDiscount%d", i);
1578 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1580 strprintf(¶m, "SinglePrice%d", i);
1581 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1584 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1585 cf.WriteDouble("Fee", td.tariffConf.fee);
1586 cf.WriteDouble("Free", td.tariffConf.free);
1587 cf.WriteString("TraffType", TARIFF::TraffTypeToString(td.tariffConf.traffType));
1588 cf.WriteString("Period", TARIFF::PeriodToString(td.tariffConf.period));
1589 cf.WriteString("ChangePolicy", TARIFF::ChangePolicyToString(td.tariffConf.changePolicy));
1590 cf.WriteTime("ChangePolicyTimeout", td.tariffConf.changePolicyTimeout);
1595 //-----------------------------------------------------------------------------*/
1596 int FILES_STORE::AddService(const std::string & name) const
1598 std::string fileName;
1599 strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str());
1601 if (Touch(fileName))
1603 STG_LOCKER lock(&mutex);
1604 errorStr = "Cannot create file " + fileName;
1605 printfd(__FILE__, "FILES_STORE::AddService - failed to add service '%s'\n", name.c_str());
1611 //-----------------------------------------------------------------------------*/
1612 int FILES_STORE::DelService(const std::string & name) const
1614 std::string fileName;
1615 strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str());
1616 if (unlink(fileName.c_str()))
1618 STG_LOCKER lock(&mutex);
1619 errorStr = "unlink failed. Message: '";
1620 errorStr += strerror(errno);
1622 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1626 //-----------------------------------------------------------------------------*/
1627 int FILES_STORE::SaveService(const SERVICE_CONF & conf) const
1629 std::string fileName;
1631 strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), conf.name.c_str());
1634 CONFIGFILE cf(fileName, true);
1640 STG_LOCKER lock(&mutex);
1641 errorStr = "Cannot write service " + conf.name + ". " + fileName;
1642 printfd(__FILE__, "FILES_STORE::SaveService - failed to save service '%s'\n", conf.name.c_str());
1646 cf.WriteString("name", conf.name);
1647 cf.WriteString("comment", conf.comment);
1648 cf.WriteDouble("cost", conf.cost);
1649 cf.WriteInt("pay_day", conf.payDay);
1654 //-----------------------------------------------------------------------------
1655 int FILES_STORE::RestoreService(SERVICE_CONF * conf, const std::string & name) const
1657 std::string fileName;
1658 strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str());
1659 CONFIGFILE cf(fileName);
1663 STG_LOCKER lock(&mutex);
1664 errorStr = "Cannot open " + fileName;
1665 printfd(__FILE__, "FILES_STORE::RestoreService - failed to restore service '%s'\n", name.c_str());
1669 if (cf.ReadString("name", &conf->name, name))
1671 STG_LOCKER lock(&mutex);
1672 errorStr = "Error in parameter 'name'";
1673 printfd(__FILE__, "FILES_STORE::RestoreService - name read failed for service '%s'\n", name.c_str());
1677 if (cf.ReadString("comment", &conf->comment, ""))
1679 STG_LOCKER lock(&mutex);
1680 errorStr = "Error in parameter 'comment'";
1681 printfd(__FILE__, "FILES_STORE::RestoreService - comment read failed for service '%s'\n", name.c_str());
1685 if (cf.ReadDouble("cost", &conf->cost, 0.0))
1687 STG_LOCKER lock(&mutex);
1688 errorStr = "Error in parameter 'cost'";
1689 printfd(__FILE__, "FILES_STORE::RestoreService - cost read failed for service '%s'\n", name.c_str());
1693 unsigned short value = 0;
1694 if (cf.ReadUShortInt("pay_day", &value, 0))
1696 STG_LOCKER lock(&mutex);
1697 errorStr = "Error in parameter 'pay_day'";
1698 printfd(__FILE__, "FILES_STORE::RestoreService - pay day read failed for service '%s'\n", name.c_str());
1701 conf->payDay = value;
1705 //-----------------------------------------------------------------------------
1706 int FILES_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree,
1708 const std::string & login) const
1710 char fn[FN_STR_LEN];
1711 char dn[FN_STR_LEN];
1718 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1719 if (access(dn, F_OK) != 0)
1721 if (mkdir(dn, 0700) != 0)
1723 STG_LOCKER lock(&mutex);
1724 errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1725 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1730 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1731 e += chmod(dn, storeSettings.GetStatModeDir());
1735 STG_LOCKER lock(&mutex);
1736 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1741 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1747 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1748 storeSettings.GetUsersDir().c_str(),
1752 if (access(dn, F_OK) != 0)
1754 if (mkdir(dn, 0700) != 0)
1756 STG_LOCKER lock(&mutex);
1757 errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1758 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1763 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1764 e += chmod(dn, storeSettings.GetStatModeDir());
1768 STG_LOCKER lock(&mutex);
1769 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1772 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1773 storeSettings.GetUsersDir().c_str(),
1776 lt->tm_mon+1 < 10 ? "0" : "",
1778 if (access(dn, F_OK) != 0)
1780 if (mkdir(dn, 0700) != 0)
1782 STG_LOCKER lock(&mutex);
1783 errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1784 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1789 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1790 e += chmod(dn, storeSettings.GetStatModeDir());
1794 STG_LOCKER lock(&mutex);
1795 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1798 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1800 statFile = fopen (fn, "at");
1804 STG_LOCKER lock(&mutex);
1805 errorStr = "File \'" + std::string(fn) + "\' cannot be written.";
1806 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1813 lt1 = localtime(&lastStat);
1822 lt2 = localtime(&t);
1828 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1829 h1, m1, s1, h2, m2, s2) < 0)
1831 STG_LOCKER lock(&mutex);
1832 errorStr = std::string("fprint failed. Message: '") + strerror(errno) + "'";
1833 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1838 std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1839 stIter = statTree.begin();
1841 while (stIter != statTree.end())
1844 x2str(stIter->second.up, u);
1845 x2str(stIter->second.down, d);
1846 #ifdef TRAFF_STAT_WITH_PORTS
1847 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1848 inet_ntostring(stIter->first.ip).c_str(),
1853 stIter->second.cash) < 0)
1855 STG_LOCKER lock(&mutex);
1856 errorStr = "fprint failed. Message: '";
1857 errorStr += strerror(errno);
1859 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1864 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1865 inet_ntostring(stIter->first.ip).c_str(),
1869 stIter->second.cash) < 0)
1871 STG_LOCKER lock(&mutex);
1872 errorStr = std::string("fprint failed. Message: '");
1873 errorStr += strerror(errno);
1875 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1886 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1887 e += chmod(fn, storeSettings.GetStatMode());
1891 STG_LOCKER lock(&mutex);
1892 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1897 //-----------------------------------------------------------------------------
1898 int FILES_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
1904 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1905 if (access(dn.c_str(), F_OK) != 0)
1907 if (mkdir(dn.c_str(), 0700) != 0)
1909 STG_LOCKER lock(&mutex);
1910 errorStr = "Directory \'";
1912 errorStr += "\' cannot be created.";
1913 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1918 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1920 gettimeofday(&tv, NULL);
1922 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1923 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1927 STG_LOCKER lock(&mutex);
1928 errorStr = "File \'";
1930 errorStr += "\' cannot be writen.";
1931 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1935 return EditMessage(*msg, login);
1937 //-----------------------------------------------------------------------------
1938 int FILES_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const
1940 std::string fileName;
1943 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1945 if (access(fileName.c_str(), F_OK) != 0)
1948 x2str(msg.header.id, idstr);
1949 STG_LOCKER lock(&mutex);
1950 errorStr = "Message for user \'";
1951 errorStr += login + "\' with ID \'";
1952 errorStr += idstr + "\' does not exist.";
1953 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
1957 Touch(fileName + ".new");
1959 msgFile = fopen((fileName + ".new").c_str(), "wt");
1962 STG_LOCKER lock(&mutex);
1963 errorStr = "File \'" + fileName + "\' cannot be writen.";
1964 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1969 res &= (fprintf(msgFile, "%u\n", msg.header.type) >= 0);
1970 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1971 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1972 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1973 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1974 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1975 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1979 STG_LOCKER lock(&mutex);
1980 errorStr = std::string("fprintf failed. Message: '") + strerror(errno) + "'";
1981 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
1988 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
1990 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1992 STG_LOCKER lock(&mutex);
1993 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1994 printfd(__FILE__, "FILES_STORE::EditMessage - rename failed. Message: '%s'\n", strerror(errno));
2000 //-----------------------------------------------------------------------------
2001 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const
2004 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2005 msg->header.id = id;
2006 return ReadMessage(fn, &msg->header, &msg->text);
2008 //-----------------------------------------------------------------------------
2009 int FILES_STORE::DelMessage(uint64_t id, const std::string & login) const
2012 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2014 return unlink(fn.c_str());
2016 //-----------------------------------------------------------------------------
2017 int FILES_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const
2019 std::string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/");
2021 if (access(dn.c_str(), F_OK) != 0)
2026 std::vector<std::string> messages;
2027 GetFileList(&messages, dn, S_IFREG, "");
2029 for (unsigned i = 0; i < messages.size(); i++)
2031 unsigned long long id = 0;
2033 if (str2x(messages[i].c_str(), id))
2035 if (unlink((dn + messages[i]).c_str()))
2037 STG_LOCKER lock(&mutex);
2038 errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'";
2039 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2046 if (ReadMessage(dn + messages[i], &hdr, NULL))
2053 if (unlink((dn + messages[i]).c_str()))
2055 STG_LOCKER lock(&mutex);
2056 errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'";
2057 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2064 hdrsList->push_back(hdr);
2068 //-----------------------------------------------------------------------------
2069 int FILES_STORE::ReadMessage(const std::string & fileName,
2071 std::string * text) const
2074 msgFile = fopen(fileName.c_str(), "rt");
2077 STG_LOCKER lock(&mutex);
2078 errorStr = "File \'";
2079 errorStr += fileName;
2080 errorStr += "\' cannot be openned.";
2081 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
2087 d[1] = &hdr->lastSendTime;
2088 d[2] = &hdr->creationTime;
2089 d[3] = &hdr->showTime;
2090 d[4] = (unsigned*)(&hdr->repeat);
2091 d[5] = &hdr->repeatPeriod;
2093 memset(p, 0, sizeof(p));
2095 for (int pos = 0; pos < 6; pos++)
2097 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
2098 STG_LOCKER lock(&mutex);
2099 errorStr = "Cannot read file \'";
2100 errorStr += fileName;
2101 errorStr += "\'. Missing data.";
2102 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
2103 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2109 ep = strrchr(p, '\r');
2111 ep = strrchr(p, '\n');
2116 STG_LOCKER lock(&mutex);
2117 errorStr = "Cannot read file \'";
2118 errorStr += fileName;
2119 errorStr += "\'. Missing data.";
2120 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
2121 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2126 if (str2x(p, *(d[pos])))
2128 STG_LOCKER lock(&mutex);
2129 errorStr = "Cannot read file \'";
2130 errorStr += fileName;
2131 errorStr += "\'. Incorrect value. \'";
2134 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
2141 memset(txt, 0, sizeof(txt));
2144 text->erase(text->begin(), text->end());
2145 while (!feof(msgFile))
2148 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2158 //-----------------------------------------------------------------------------
2159 int FILES_STORE::Touch(const std::string & path) const
2161 FILE * f = fopen(path.c_str(), "wb");
2169 //-----------------------------------------------------------------------------
2170 int GetFileList(std::vector<std::string> * fileList, const std::string & directory, mode_t mode, const std::string & ext)
2172 DIR * d = opendir(directory.c_str());
2176 printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno));
2181 while ((entry = readdir(d)))
2183 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
2186 std::string str = directory + "/" + std::string(entry->d_name);
2189 if (stat(str.c_str(), &st))
2192 if (!(st.st_mode & mode)) // Filter by mode
2198 size_t d_nameLen = strlen(entry->d_name);
2199 if (d_nameLen <= ext.size())
2202 if (ext == entry->d_name + (d_nameLen - ext.size()))
2204 entry->d_name[d_nameLen - ext.size()] = 0;
2205 fileList->push_back(entry->d_name);
2210 fileList->push_back(entry->d_name);
2218 //-----------------------------------------------------------------------------