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/admin_conf.h"
41 #include "stg/tariff.h"
42 #include "stg/tariff_conf.h"
43 #include "stg/service_conf.h"
60 #define DELETED_USERS_DIR "deleted_users"
62 #define adm_enc_passwd "cjeifY8m3"
64 int GetFileList(std::vector<std::string> * fileList, const std::string & directory, mode_t mode, const std::string & ext);
66 const int pt_mega = 1024 * 1024;
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
73 bool CheckAndCreate(const std::string & dir, mode_t mode)
75 if (access(dir.c_str(), F_OK) == 0)
77 if (mkdir(dir.c_str(), mode) == 0)
84 extern "C" STG::Store* GetStore()
86 static FILES_STORE plugin;
89 //-----------------------------------------------------------------------------
90 FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
104 //-----------------------------------------------------------------------------
105 int FILES_STORE_SETTINGS::ParseOwner(const std::vector<STG::ParamValue> & moduleParams, const std::string & owner, uid_t * uid)
109 std::vector<STG::ParamValue>::const_iterator pvi;
110 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
111 if (pvi == moduleParams.end() || pvi->value.empty())
113 m_errorStr = "Parameter \'" + owner + "\' not found.";
114 printfd(__FILE__, "%s\n", m_errorStr.c_str());
117 if (User2UID(pvi->value[0].c_str(), uid) < 0)
119 m_errorStr = "Parameter \'" + owner + "\': Unknown user \'" + pvi->value[0] + "\'";
120 printfd(__FILE__, "%s\n", m_errorStr.c_str());
125 //-----------------------------------------------------------------------------
126 int FILES_STORE_SETTINGS::ParseGroup(const std::vector<STG::ParamValue> & moduleParams, const std::string & group, gid_t * gid)
130 std::vector<STG::ParamValue>::const_iterator pvi;
131 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
132 if (pvi == moduleParams.end() || pvi->value.empty())
134 m_errorStr = "Parameter \'" + group + "\' not found.";
135 printfd(__FILE__, "%s\n", m_errorStr.c_str());
138 if (Group2GID(pvi->value[0].c_str(), gid) < 0)
140 m_errorStr = "Parameter \'" + group + "\': Unknown group \'" + pvi->value[0] + "\'";
141 printfd(__FILE__, "%s\n", m_errorStr.c_str());
146 //-----------------------------------------------------------------------------
147 int FILES_STORE_SETTINGS::ParseYesNo(const std::string & value, bool * val)
149 if (0 == strcasecmp(value.c_str(), "yes"))
154 if (0 == strcasecmp(value.c_str(), "no"))
160 m_errorStr = "Incorrect value \'" + value + "\'.";
163 //-----------------------------------------------------------------------------
164 int FILES_STORE_SETTINGS::ParseMode(const std::vector<STG::ParamValue> & moduleParams, const std::string & modeStr, mode_t * mode)
168 std::vector<STG::ParamValue>::const_iterator pvi;
169 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
170 if (pvi == moduleParams.end() || pvi->value.empty())
172 m_errorStr = "Parameter \'" + modeStr + "\' not found.";
173 printfd(__FILE__, "%s\n", m_errorStr.c_str());
176 if (Str2Mode(pvi->value[0].c_str(), mode) < 0)
178 m_errorStr = "Parameter \'" + modeStr + "\': Incorrect mode \'" + pvi->value[0] + "\'";
179 printfd(__FILE__, "%s\n", m_errorStr.c_str());
184 //-----------------------------------------------------------------------------
185 int FILES_STORE_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
187 if (ParseOwner(s.moduleParams, "StatOwner", &m_statUID) < 0)
189 if (ParseGroup(s.moduleParams, "StatGroup", &m_statGID) < 0)
191 if (ParseMode(s.moduleParams, "StatMode", &m_statMode) < 0)
194 if (ParseOwner(s.moduleParams, "ConfOwner", &m_confUID) < 0)
196 if (ParseGroup(s.moduleParams, "ConfGroup", &m_confGID) < 0)
198 if (ParseMode(s.moduleParams, "ConfMode", &m_confMode) < 0)
201 if (ParseOwner(s.moduleParams, "UserLogOwner", &m_userLogUID) < 0)
203 if (ParseGroup(s.moduleParams, "UserLogGroup", &m_userLogGID) < 0)
205 if (ParseMode(s.moduleParams, "UserLogMode", &m_userLogMode) < 0)
208 std::vector<STG::ParamValue>::const_iterator pvi;
210 pv.param = "RemoveBak";
211 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
212 if (pvi == s.moduleParams.end() || pvi->value.empty())
218 if (ParseYesNo(pvi->value[0], &m_removeBak))
220 printfd(__FILE__, "Cannot parse parameter 'RemoveBak'\n");
225 pv.param = "ReadBak";
226 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
227 if (pvi == s.moduleParams.end() || pvi->value.empty())
233 if (ParseYesNo(pvi->value[0], &m_readBak))
235 printfd(__FILE__, "Cannot parse parameter 'ReadBak'\n");
240 pv.param = "WorkDir";
241 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
242 if (pvi == s.moduleParams.end() || pvi->value.empty())
244 m_errorStr = "Parameter \'WorkDir\' not found.";
245 printfd(__FILE__, "Parameter 'WorkDir' not found\n");
249 m_workDir = pvi->value[0];
250 if (m_workDir.size() && m_workDir[m_workDir.size() - 1] == '/')
252 m_workDir.resize(m_workDir.size() - 1);
254 m_usersDir = m_workDir + "/users/";
255 if (!CheckAndCreate(m_usersDir, GetConfModeDir()))
257 m_errorStr = m_usersDir + " doesn't exist. Failed to create.";
258 printfd(__FILE__, "%s\n", m_errorStr.c_str());
261 m_tariffsDir = m_workDir + "/tariffs/";
262 if (!CheckAndCreate(m_tariffsDir, GetConfModeDir()))
264 m_errorStr = m_tariffsDir + " doesn't exist. Failed to create.";
265 printfd(__FILE__, "%s\n", m_errorStr.c_str());
268 m_adminsDir = m_workDir + "/admins/";
269 if (!CheckAndCreate(m_adminsDir, GetConfModeDir()))
271 m_errorStr = m_adminsDir + " doesn't exist. Failed to create.";
272 printfd(__FILE__, "%s\n", m_errorStr.c_str());
275 m_servicesDir = m_workDir + "/services/";
276 if (!CheckAndCreate(m_servicesDir, GetConfModeDir()))
278 m_errorStr = m_servicesDir + " doesn't exist. Failed to create.";
279 printfd(__FILE__, "%s\n", m_errorStr.c_str());
285 //-----------------------------------------------------------------------------
286 const std::string & FILES_STORE_SETTINGS::GetStrError() const
290 //-----------------------------------------------------------------------------
291 int FILES_STORE_SETTINGS::User2UID(const char * user, uid_t * uid)
297 m_errorStr = std::string("User \'") + std::string(user) + std::string("\' not found in system.");
298 printfd(__FILE__, "%s\n", m_errorStr.c_str());
305 //-----------------------------------------------------------------------------
306 int FILES_STORE_SETTINGS::Group2GID(const char * gr, gid_t * gid)
312 m_errorStr = std::string("Group \'") + std::string(gr) + std::string("\' not found in system.");
313 printfd(__FILE__, "%s\n", m_errorStr.c_str());
320 //-----------------------------------------------------------------------------
321 int FILES_STORE_SETTINGS::Str2Mode(const char * str, mode_t * mode)
325 m_errorStr = std::string("Error parsing mode \'") + str + std::string("\'");
326 printfd(__FILE__, "%s\n", m_errorStr.c_str());
330 for (int i = 0; i < 3; i++)
331 if (str[i] > '7' || str[i] < '0')
333 m_errorStr = std::string("Error parsing mode \'") + str + std::string("\'");
334 printfd(__FILE__, "%s\n", m_errorStr.c_str());
338 mode_t a = str[0] - '0';
339 mode_t b = str[1] - '0';
340 mode_t c = str[2] - '0';
342 *mode = c + (b << 3) + (a << 6);
346 //-----------------------------------------------------------------------------
347 mode_t FILES_STORE_SETTINGS::GetStatModeDir() const
349 mode_t mode = m_statMode;
350 if (m_statMode & S_IRUSR) mode |= S_IXUSR;
351 if (m_statMode & S_IRGRP) mode |= S_IXGRP;
352 if (m_statMode & S_IROTH) mode |= S_IXOTH;
355 //-----------------------------------------------------------------------------
356 mode_t FILES_STORE_SETTINGS::GetConfModeDir() const
358 mode_t mode = m_confMode;
359 if (m_confMode & S_IRUSR) mode |= S_IXUSR;
360 if (m_confMode & S_IRGRP) mode |= S_IXGRP;
361 if (m_confMode & S_IROTH) mode |= S_IXOTH;
364 //-----------------------------------------------------------------------------
365 //-----------------------------------------------------------------------------
366 //-----------------------------------------------------------------------------
367 FILES_STORE::FILES_STORE()
368 : m_version("file_store v.1.04"),
369 m_logger(STG::PluginLogger::get("store_files"))
372 //-----------------------------------------------------------------------------
373 int FILES_STORE::ParseSettings()
375 int ret = m_storeSettings.ParseSettings(m_settings);
378 std::lock_guard lock(m_mutex);
379 m_errorStr = m_storeSettings.GetStrError();
383 //-----------------------------------------------------------------------------
384 int FILES_STORE::GetUsersList(std::vector<std::string> * userList) const
386 std::vector<std::string> files;
388 if (GetFileList(&files, m_storeSettings.GetUsersDir(), S_IFDIR, ""))
390 std::lock_guard lock(m_mutex);
391 m_errorStr = "Failed to open '" + m_storeSettings.GetUsersDir() + "': " + std::string(strerror(errno));
395 std::lock_guard lock(m_mutex);
397 userList->swap(files);
401 //-----------------------------------------------------------------------------
402 int FILES_STORE::GetAdminsList(std::vector<std::string> * adminList) const
404 std::vector<std::string> files;
406 if (GetFileList(&files, m_storeSettings.GetAdminsDir(), S_IFREG, ".adm"))
408 std::lock_guard lock(m_mutex);
409 m_errorStr = "Failed to open '" + m_storeSettings.GetAdminsDir() + "': " + std::string(strerror(errno));
413 std::lock_guard lock(m_mutex);
415 adminList->swap(files);
419 //-----------------------------------------------------------------------------
420 int FILES_STORE::GetTariffsList(std::vector<std::string> * tariffList) const
422 std::vector<std::string> files;
424 if (GetFileList(&files, m_storeSettings.GetTariffsDir(), S_IFREG, ".tf"))
426 std::lock_guard lock(m_mutex);
427 m_errorStr = "Failed to open '" + m_storeSettings.GetTariffsDir() + "': " + std::string(strerror(errno));
431 std::lock_guard lock(m_mutex);
433 tariffList->swap(files);
437 //-----------------------------------------------------------------------------
438 int FILES_STORE::GetServicesList(std::vector<std::string> * list) const
440 std::vector<std::string> files;
442 if (GetFileList(&files, m_storeSettings.GetServicesDir(), S_IFREG, ".serv"))
444 std::lock_guard lock(m_mutex);
445 m_errorStr = "Failed to open '" + m_storeSettings.GetServicesDir() + "': " + std::string(strerror(errno));
449 std::lock_guard lock(m_mutex);
455 //-----------------------------------------------------------------------------
456 int FILES_STORE::RemoveDir(const char * path) const
458 DIR * d = opendir(path);
462 m_errorStr = "failed to open dir. Message: '";
463 m_errorStr += strerror(errno);
465 printfd(__FILE__, "FILE_STORE::RemoveDir() - Failed to open dir '%s': '%s'\n", path, strerror(errno));
470 while ((entry = readdir(d)))
472 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
475 std::string str = path;
476 str += "/" + std::string(entry->d_name);
479 if (stat(str.c_str(), &st))
482 if ((st.st_mode & S_IFREG))
484 if (unlink(str.c_str()))
486 std::lock_guard lock(m_mutex);
487 m_errorStr = "unlink failed. Message: '";
488 m_errorStr += strerror(errno);
490 printfd(__FILE__, "FILES_STORE::RemoveDir() - unlink failed. Message: '%s'\n", strerror(errno));
496 if (!(st.st_mode & S_IFDIR))
498 if (RemoveDir(str.c_str()))
511 std::lock_guard lock(m_mutex);
512 m_errorStr = "rmdir failed. Message: '";
513 m_errorStr += strerror(errno);
515 printfd(__FILE__, "FILES_STORE::RemoveDir() - rmdir failed. Message: '%s'\n", strerror(errno));
521 //-----------------------------------------------------------------------------
522 int FILES_STORE::AddUser(const std::string & login) const
524 std::string fileName;
526 strprintf(&fileName, "%s%s", m_storeSettings.GetUsersDir().c_str(), login.c_str());
528 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
530 std::lock_guard lock(m_mutex);
531 m_errorStr = std::string("mkdir failed. Message: '") + strerror(errno) + "'";
532 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
536 strprintf(&fileName, "%s%s/conf", m_storeSettings.GetUsersDir().c_str(), login.c_str());
539 std::lock_guard lock(m_mutex);
540 m_errorStr = "Cannot create file \"" + fileName + "\'";
541 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
545 strprintf(&fileName, "%s%s/stat", m_storeSettings.GetUsersDir().c_str(), login.c_str());
548 std::lock_guard lock(m_mutex);
549 m_errorStr = "Cannot create file \"" + fileName + "\'";
550 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
555 //-----------------------------------------------------------------------------
556 int FILES_STORE::DelUser(const std::string & login) const
559 std::string dirName1;
561 strprintf(&dirName, "%s/%s", m_storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR);
562 if (access(dirName.c_str(), F_OK) != 0)
564 if (mkdir(dirName.c_str(), 0700) != 0)
566 std::lock_guard lock(m_mutex);
567 m_errorStr = "Directory '" + dirName + "' cannot be created.";
568 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
573 if (access(dirName.c_str(), F_OK) == 0)
575 strprintf(&dirName, "%s/%s/%s.%lu", m_storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR, login.c_str(), time(NULL));
576 strprintf(&dirName1, "%s/%s", m_storeSettings.GetUsersDir().c_str(), login.c_str());
577 if (rename(dirName1.c_str(), dirName.c_str()))
579 std::lock_guard lock(m_mutex);
580 m_errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
581 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
587 strprintf(&dirName, "%s/%s", m_storeSettings.GetUsersDir().c_str(), login.c_str());
588 if (RemoveDir(dirName.c_str()))
595 //-----------------------------------------------------------------------------
596 int FILES_STORE::RestoreUserConf(STG::UserConf * conf, const std::string & login) const
598 std::string fileName;
599 fileName = m_storeSettings.GetUsersDir() + "/" + login + "/conf";
600 if (RestoreUserConf(conf, login, fileName))
602 if (!m_storeSettings.GetReadBak())
606 return RestoreUserConf(conf, login, fileName + ".bak");
610 //-----------------------------------------------------------------------------
611 int FILES_STORE::RestoreUserConf(STG::UserConf * conf, const std::string & login, const std::string & fileName) const
613 CONFIGFILE cf(fileName);
618 std::lock_guard lock(m_mutex);
619 m_errorStr = "User \'" + login + "\' data not read.";
620 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
624 if (cf.ReadString("Password", &conf->password, "") < 0)
626 std::lock_guard lock(m_mutex);
627 m_errorStr = "User \'" + login + "\' data not read. Parameter Password.";
628 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
631 if (conf->password.empty())
633 std::lock_guard lock(m_mutex);
634 m_errorStr = "User \'" + login + "\' password is blank.";
635 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
639 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
641 std::lock_guard lock(m_mutex);
642 m_errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
643 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
646 if (conf->tariffName.empty())
648 std::lock_guard lock(m_mutex);
649 m_errorStr = "User \'" + login + "\' tariff is blank.";
650 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
655 cf.ReadString("IP", &ipStr, "?");
658 conf->ips = STG::UserIPs::parse(ipStr);
660 catch (const std::string & s)
662 std::lock_guard lock(m_mutex);
663 m_errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
664 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
668 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
670 std::lock_guard lock(m_mutex);
671 m_errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
672 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
676 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
678 std::lock_guard lock(m_mutex);
679 m_errorStr = "User \'" + login + "\' data not read. Parameter Down.";
680 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
684 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
686 std::lock_guard lock(m_mutex);
687 m_errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
688 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
692 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
693 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
694 cf.ReadString("TariffChange", &conf->nextTariff, "");
695 cf.ReadString("Group", &conf->group, "");
696 cf.ReadString("RealName", &conf->realName, "");
697 cf.ReadString("Address", &conf->address, "");
698 cf.ReadString("Phone", &conf->phone, "");
699 cf.ReadString("Note", &conf->note, "");
700 cf.ReadString("email", &conf->email, "");
702 char userdataName[12];
703 for (int i = 0; i < USERDATA_NUM; i++)
705 snprintf(userdataName, 12, "Userdata%d", i);
706 cf.ReadString(userdataName, &conf->userdata[i], "");
709 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
711 std::lock_guard lock(m_mutex);
712 m_errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
713 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
719 //-----------------------------------------------------------------------------
720 int FILES_STORE::RestoreUserStat(STG::UserStat * stat, const std::string & login) const
722 std::string fileName;
723 fileName = m_storeSettings.GetUsersDir() + "/" + login + "/stat";
725 if (RestoreUserStat(stat, login, fileName))
727 if (!m_storeSettings.GetReadBak())
731 return RestoreUserStat(stat, login, fileName + ".bak");
735 //-----------------------------------------------------------------------------
736 int FILES_STORE::RestoreUserStat(STG::UserStat * stat, const std::string & login, const std::string & fileName) const
738 CONFIGFILE cf(fileName);
744 std::lock_guard lock(m_mutex);
745 m_errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
746 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
752 for (int i = 0; i < DIR_NUM; i++)
755 snprintf(s, 22, "D%d", i);
756 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
758 std::lock_guard lock(m_mutex);
759 m_errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
760 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
763 stat->monthDown[i] = traff;
765 snprintf(s, 22, "U%d", i);
766 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
768 std::lock_guard lock(m_mutex);
769 m_errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
770 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
773 stat->monthUp[i] = traff;
776 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
778 std::lock_guard lock(m_mutex);
779 m_errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
780 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
784 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
786 std::lock_guard lock(m_mutex);
787 m_errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
788 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
792 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
794 std::lock_guard lock(m_mutex);
795 m_errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
796 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
800 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
802 std::lock_guard lock(m_mutex);
803 m_errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
804 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
808 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
810 std::lock_guard lock(m_mutex);
811 m_errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
812 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
816 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
818 std::lock_guard lock(m_mutex);
819 m_errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
820 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
826 //-----------------------------------------------------------------------------
827 int FILES_STORE::SaveUserConf(const STG::UserConf & conf, const std::string & login) const
829 std::string fileName;
830 fileName = m_storeSettings.GetUsersDir() + "/" + login + "/conf";
832 CONFIGFILE cfstat(fileName, true);
834 int e = cfstat.Error();
838 std::lock_guard lock(m_mutex);
839 m_errorStr = std::string("User \'") + login + "\' conf not written\n";
840 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
844 e = chmod(fileName.c_str(), m_storeSettings.GetConfMode());
845 e += chown(fileName.c_str(), m_storeSettings.GetConfUID(), m_storeSettings.GetConfGID());
849 std::lock_guard lock(m_mutex);
850 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
853 cfstat.WriteString("Password", conf.password);
854 cfstat.WriteInt ("Passive", conf.passive);
855 cfstat.WriteInt ("Down", conf.disabled);
856 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
857 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
858 cfstat.WriteString("Tariff", conf.tariffName);
859 cfstat.WriteString("Address", conf.address);
860 cfstat.WriteString("Phone", conf.phone);
861 cfstat.WriteString("Email", conf.email);
862 cfstat.WriteString("Note", conf.note);
863 cfstat.WriteString("RealName", conf.realName);
864 cfstat.WriteString("Group", conf.group);
865 cfstat.WriteDouble("Credit", conf.credit);
866 cfstat.WriteString("TariffChange", conf.nextTariff);
868 char userdataName[12];
869 for (int i = 0; i < USERDATA_NUM; i++)
871 snprintf(userdataName, 12, "Userdata%d", i);
872 cfstat.WriteString(userdataName, conf.userdata[i]);
874 cfstat.WriteInt("CreditExpire", conf.creditExpire);
876 std::ostringstream ipStr;
878 cfstat.WriteString("IP", ipStr.str());
882 //-----------------------------------------------------------------------------
883 int FILES_STORE::SaveUserStat(const STG::UserStat & stat, const std::string & login) const
885 std::string fileName;
886 fileName = m_storeSettings.GetUsersDir() + "/" + login + "/stat";
889 CONFIGFILE cfstat(fileName, true);
890 int e = cfstat.Error();
894 std::lock_guard lock(m_mutex);
895 m_errorStr = std::string("User \'") + login + "\' stat not written\n";
896 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
900 for (int i = 0; i < DIR_NUM; i++)
903 snprintf(s, 22, "D%d", i);
904 cfstat.WriteInt(s, stat.monthDown[i]);
905 snprintf(s, 22, "U%d", i);
906 cfstat.WriteInt(s, stat.monthUp[i]);
909 cfstat.WriteDouble("Cash", stat.cash);
910 cfstat.WriteDouble("FreeMb", stat.freeMb);
911 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
912 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
913 cfstat.WriteInt("PassiveTime", stat.passiveTime);
914 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
917 int e = chmod(fileName.c_str(), m_storeSettings.GetStatMode());
918 e += chown(fileName.c_str(), m_storeSettings.GetStatUID(), m_storeSettings.GetStatGID());
922 std::lock_guard lock(m_mutex);
923 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
928 //-----------------------------------------------------------------------------
929 int FILES_STORE::WriteLogString(const std::string & str, const std::string & login) const
932 time_t tm = time(NULL);
933 std::string fileName;
934 fileName = m_storeSettings.GetUsersDir() + "/" + login + "/log";
935 f = fopen(fileName.c_str(), "at");
939 fprintf(f, "%s", LogDate(tm));
941 fprintf(f, "%s", str.c_str());
947 std::lock_guard lock(m_mutex);
948 m_errorStr = "Cannot open \'" + fileName + "\'";
949 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
953 int e = chmod(fileName.c_str(), m_storeSettings.GetLogMode());
954 e += chown(fileName.c_str(), m_storeSettings.GetLogUID(), m_storeSettings.GetLogGID());
958 std::lock_guard lock(m_mutex);
959 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
964 //-----------------------------------------------------------------------------
965 int FILES_STORE::WriteLog2String(const std::string & str, const std::string & login) const
968 time_t tm = time(NULL);
969 std::string fileName;
970 fileName = m_storeSettings.GetUsersDir() + "/" + login + "/log2";
971 f = fopen(fileName.c_str(), "at");
975 fprintf(f, "%s", LogDate(tm));
977 fprintf(f, "%s", str.c_str());
983 std::lock_guard lock(m_mutex);
984 m_errorStr = "Cannot open \'" + fileName + "\'";
985 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
989 int e = chmod(fileName.c_str(), m_storeSettings.GetLogMode());
990 e += chown(fileName.c_str(), m_storeSettings.GetLogUID(), m_storeSettings.GetLogGID());
994 std::lock_guard lock(m_mutex);
995 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1000 //-----------------------------------------------------------------------------
1001 int FILES_STORE::WriteUserChgLog(const std::string & login,
1002 const std::string & admLogin,
1004 const std::string & paramName,
1005 const std::string & oldValue,
1006 const std::string & newValue,
1007 const std::string & message) const
1009 std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1010 + paramName + "\' parameter changed from \'" + oldValue +
1011 "\' to \'" + newValue + "\'. " + message;
1013 return WriteLogString(userLogMsg, login);
1015 //-----------------------------------------------------------------------------
1016 int FILES_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
1018 std::string logStr = "Connect, " + inet_ntostring(ip);
1019 if (WriteLogString(logStr, login))
1021 return WriteLog2String(logStr, login);
1023 //-----------------------------------------------------------------------------
1024 int FILES_STORE::WriteUserDisconnect(const std::string & login,
1025 const STG::DirTraff & monthUp,
1026 const STG::DirTraff & monthDown,
1027 const STG::DirTraff & sessionUp,
1028 const STG::DirTraff & sessionDown,
1031 const std::string & reason) const
1033 std::ostringstream logStr;
1034 logStr << "Disconnect, "
1035 << " session upload: \'"
1037 << "\' session download: \'"
1039 << "\' month upload: \'"
1041 << "\' month download: \'"
1047 if (WriteLogString(logStr.str(), login))
1050 logStr << " freeMb: \'"
1057 return WriteLog2String(logStr.str(), login);
1059 //-----------------------------------------------------------------------------
1060 int FILES_STORE::SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string & login) const
1064 strprintf(&stat1,"%s/%s/stat.%d.%02d",
1065 m_storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1067 CONFIGFILE s(stat1, true);
1071 std::lock_guard lock(m_mutex);
1072 m_errorStr = "Cannot create file '" + stat1 + "'";
1073 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1079 strprintf(&stat2,"%s/%s/stat2.%d.%02d",
1080 m_storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1082 CONFIGFILE s2(stat2, true);
1086 std::lock_guard lock(m_mutex);
1087 m_errorStr = "Cannot create file '" + stat2 + "'";
1088 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1092 for (size_t i = 0; i < DIR_NUM; i++)
1095 snprintf(dirName, 3, "U%llu", i);
1096 s.WriteInt(dirName, stat.monthUp[i]); // Classic
1097 s2.WriteInt(dirName, stat.monthUp[i]); // New
1098 snprintf(dirName, 3, "D%llu", i);
1099 s.WriteInt(dirName, stat.monthDown[i]); // Classic
1100 s2.WriteInt(dirName, stat.monthDown[i]); // New
1104 s.WriteDouble("cash", stat.cash);
1107 s2.WriteDouble("Cash", stat.cash);
1108 s2.WriteDouble("FreeMb", stat.freeMb);
1109 s2.WriteDouble("LastCashAdd", stat.lastCashAdd);
1110 s2.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1111 s2.WriteInt("PassiveTime", stat.passiveTime);
1112 s2.WriteInt("LastActivityTime", stat.lastActivityTime);
1116 //-----------------------------------------------------------------------------*/
1117 int FILES_STORE::AddAdmin(const std::string & login) const
1119 std::string fileName;
1120 strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), login.c_str());
1122 if (Touch(fileName))
1124 std::lock_guard lock(m_mutex);
1125 m_errorStr = "Cannot create file " + fileName;
1126 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1132 //-----------------------------------------------------------------------------*/
1133 int FILES_STORE::DelAdmin(const std::string & login) const
1135 std::string fileName;
1136 strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), login.c_str());
1137 if (unlink(fileName.c_str()))
1139 std::lock_guard lock(m_mutex);
1140 m_errorStr = "unlink failed. Message: '";
1141 m_errorStr += strerror(errno);
1143 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1147 //-----------------------------------------------------------------------------*/
1148 int FILES_STORE::SaveAdmin(const STG::AdminConf & ac) const
1150 std::string fileName;
1152 strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1155 CONFIGFILE cf(fileName, true);
1161 std::lock_guard lock(m_mutex);
1162 m_errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1163 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1167 char pass[ADM_PASSWD_LEN + 1];
1168 memset(pass, 0, sizeof(pass));
1170 char adminPass[ADM_PASSWD_LEN + 1];
1171 memset(adminPass, 0, sizeof(adminPass));
1174 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1176 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1177 adminPass[ADM_PASSWD_LEN - 1] = 0;
1179 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1181 EncryptBlock(pass + 8*i, adminPass + 8*i, &ctx);
1184 pass[ADM_PASSWD_LEN - 1] = 0;
1185 char passwordE[2 * ADM_PASSWD_LEN + 2];
1186 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1188 cf.WriteString("password", passwordE);
1189 cf.WriteInt("ChgConf", ac.priv.userConf);
1190 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1191 cf.WriteInt("ChgStat", ac.priv.userStat);
1192 cf.WriteInt("ChgCash", ac.priv.userCash);
1193 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1194 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1195 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1196 cf.WriteInt("ChgService", ac.priv.serviceChg);
1197 cf.WriteInt("ChgCorp", ac.priv.corpChg);
1202 //-----------------------------------------------------------------------------
1203 int FILES_STORE::RestoreAdmin(STG::AdminConf * ac, const std::string & login) const
1205 std::string fileName;
1206 strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), login.c_str());
1207 CONFIGFILE cf(fileName);
1208 char pass[ADM_PASSWD_LEN + 1];
1209 char password[ADM_PASSWD_LEN + 1];
1210 char passwordE[2 * ADM_PASSWD_LEN + 2];
1217 std::lock_guard lock(m_mutex);
1218 m_errorStr = "Cannot open " + fileName;
1219 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1223 if (cf.ReadString("password", &p, "*"))
1225 std::lock_guard lock(m_mutex);
1226 m_errorStr = "Error in parameter password";
1227 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1231 memset(passwordE, 0, sizeof(passwordE));
1232 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1234 memset(pass, 0, sizeof(pass));
1236 if (passwordE[0] != 0)
1238 Decode21(pass, passwordE);
1239 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1241 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1243 DecryptBlock(password + 8*i, pass + 8*i, &ctx);
1251 ac->password = password;
1255 if (cf.ReadUShortInt("ChgConf", &a, 0) == 0)
1256 ac->priv.userConf = a;
1259 std::lock_guard lock(m_mutex);
1260 m_errorStr = "Error in parameter ChgConf";
1261 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1265 if (cf.ReadUShortInt("ChgPassword", &a, 0) == 0)
1266 ac->priv.userPasswd = a;
1269 std::lock_guard lock(m_mutex);
1270 m_errorStr = "Error in parameter ChgPassword";
1271 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1275 if (cf.ReadUShortInt("ChgStat", &a, 0) == 0)
1276 ac->priv.userStat = a;
1279 std::lock_guard lock(m_mutex);
1280 m_errorStr = "Error in parameter ChgStat";
1281 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1285 if (cf.ReadUShortInt("ChgCash", &a, 0) == 0)
1286 ac->priv.userCash = a;
1289 std::lock_guard lock(m_mutex);
1290 m_errorStr = "Error in parameter ChgCash";
1291 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1295 if (cf.ReadUShortInt("UsrAddDel", &a, 0) == 0)
1296 ac->priv.userAddDel = a;
1299 std::lock_guard lock(m_mutex);
1300 m_errorStr = "Error in parameter UsrAddDel";
1301 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1305 if (cf.ReadUShortInt("ChgAdmin", &a, 0) == 0)
1306 ac->priv.adminChg = a;
1309 std::lock_guard lock(m_mutex);
1310 m_errorStr = "Error in parameter ChgAdmin";
1311 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1315 if (cf.ReadUShortInt("ChgTariff", &a, 0) == 0)
1316 ac->priv.tariffChg = a;
1319 std::lock_guard lock(m_mutex);
1320 m_errorStr = "Error in parameter ChgTariff";
1321 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1325 if (cf.ReadUShortInt("ChgService", &a, 0) == 0)
1326 ac->priv.serviceChg = a;
1328 ac->priv.serviceChg = 0;
1330 if (cf.ReadUShortInt("ChgCorp", &a, 0) == 0)
1331 ac->priv.corpChg = a;
1333 ac->priv.corpChg = 0;
1337 //-----------------------------------------------------------------------------
1338 int FILES_STORE::AddTariff(const std::string & name) const
1340 std::string fileName;
1341 strprintf(&fileName, "%s/%s.tf", m_storeSettings.GetTariffsDir().c_str(), name.c_str());
1342 if (Touch(fileName))
1344 std::lock_guard lock(m_mutex);
1345 m_errorStr = "Cannot create file " + fileName;
1346 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1351 //-----------------------------------------------------------------------------
1352 int FILES_STORE::DelTariff(const std::string & name) const
1354 std::string fileName;
1355 strprintf(&fileName, "%s/%s.tf", m_storeSettings.GetTariffsDir().c_str(), name.c_str());
1356 if (unlink(fileName.c_str()))
1358 std::lock_guard lock(m_mutex);
1359 m_errorStr = "unlink failed. Message: '";
1360 m_errorStr += strerror(errno);
1362 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1366 //-----------------------------------------------------------------------------
1367 int FILES_STORE::RestoreTariff(STG::TariffData * td, const std::string & tariffName) const
1369 std::string fileName = m_storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1370 CONFIGFILE conf(fileName);
1372 td->tariffConf.name = tariffName;
1374 if (conf.Error() != 0)
1376 std::lock_guard lock(m_mutex);
1377 m_errorStr = "Cannot read file " + fileName;
1378 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1383 for (int i = 0; i<DIR_NUM; i++)
1385 strprintf(¶m, "Time%d", i);
1386 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1388 std::lock_guard lock(m_mutex);
1389 m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1390 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1394 ParseTariffTimeStr(str.c_str(),
1395 td->dirPrice[i].hDay,
1396 td->dirPrice[i].mDay,
1397 td->dirPrice[i].hNight,
1398 td->dirPrice[i].mNight);
1400 strprintf(¶m, "PriceDayA%d", i);
1401 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1403 std::lock_guard lock(m_mutex);
1404 m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1405 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1408 td->dirPrice[i].priceDayA /= (1024*1024);
1410 strprintf(¶m, "PriceDayB%d", i);
1411 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1413 std::lock_guard lock(m_mutex);
1414 m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1415 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1418 td->dirPrice[i].priceDayB /= (1024*1024);
1420 strprintf(¶m, "PriceNightA%d", i);
1421 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1423 std::lock_guard lock(m_mutex);
1424 m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1425 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1428 td->dirPrice[i].priceNightA /= (1024*1024);
1430 strprintf(¶m, "PriceNightB%d", i);
1431 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1433 std::lock_guard lock(m_mutex);
1434 m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1435 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1438 td->dirPrice[i].priceNightB /= (1024*1024);
1440 strprintf(¶m, "Threshold%d", i);
1441 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1443 std::lock_guard lock(m_mutex);
1444 m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1445 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1449 strprintf(¶m, "SinglePrice%d", i);
1450 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1452 std::lock_guard lock(m_mutex);
1453 m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1454 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1458 strprintf(¶m, "NoDiscount%d", i);
1459 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1461 std::lock_guard lock(m_mutex);
1462 m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1463 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1468 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1470 std::lock_guard lock(m_mutex);
1471 m_errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1472 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1476 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1478 std::lock_guard lock(m_mutex);
1479 m_errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1480 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1484 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1486 std::lock_guard lock(m_mutex);
1487 m_errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1488 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1492 if (conf.ReadString("TraffType", &str, "") < 0)
1494 std::lock_guard lock(m_mutex);
1495 m_errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1496 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1500 td->tariffConf.traffType = STG::Tariff::parseTraffType(str);
1502 if (conf.ReadString("Period", &str, "month") < 0)
1503 td->tariffConf.period = STG::Tariff::MONTH;
1505 td->tariffConf.period = STG::Tariff::parsePeriod(str);
1507 if (conf.ReadString("ChangePolicy", &str, "allow") < 0)
1508 td->tariffConf.changePolicy = STG::Tariff::ALLOW;
1510 td->tariffConf.changePolicy = STG::Tariff::parseChangePolicy(str);
1512 conf.ReadTime("ChangePolicyTimeout", &td->tariffConf.changePolicyTimeout, 0);
1515 //-----------------------------------------------------------------------------
1516 int FILES_STORE::SaveTariff(const STG::TariffData & td, const std::string & tariffName) const
1518 std::string fileName = m_storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1521 CONFIGFILE cf(fileName, true);
1527 std::lock_guard lock(m_mutex);
1528 m_errorStr = "Error writing tariff " + tariffName;
1529 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1534 for (int i = 0; i < DIR_NUM; i++)
1536 strprintf(¶m, "PriceDayA%d", i);
1537 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1539 strprintf(¶m, "PriceDayB%d", i);
1540 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1542 strprintf(¶m, "PriceNightA%d", i);
1543 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1545 strprintf(¶m, "PriceNightB%d", i);
1546 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1548 strprintf(¶m, "Threshold%d", i);
1549 cf.WriteInt(param, td.dirPrice[i].threshold);
1552 strprintf(¶m, "Time%d", i);
1554 strprintf(&s, "%0d:%0d-%0d:%0d",
1555 td.dirPrice[i].hDay,
1556 td.dirPrice[i].mDay,
1557 td.dirPrice[i].hNight,
1558 td.dirPrice[i].mNight);
1560 cf.WriteString(param, s);
1562 strprintf(¶m, "NoDiscount%d", i);
1563 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1565 strprintf(¶m, "SinglePrice%d", i);
1566 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1569 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1570 cf.WriteDouble("Fee", td.tariffConf.fee);
1571 cf.WriteDouble("Free", td.tariffConf.free);
1572 cf.WriteString("TraffType", STG::Tariff::toString(td.tariffConf.traffType));
1573 cf.WriteString("Period", STG::Tariff::toString(td.tariffConf.period));
1574 cf.WriteString("ChangePolicy", STG::Tariff::toString(td.tariffConf.changePolicy));
1575 cf.WriteTime("ChangePolicyTimeout", td.tariffConf.changePolicyTimeout);
1580 //-----------------------------------------------------------------------------*/
1581 int FILES_STORE::AddService(const std::string & name) const
1583 std::string fileName;
1584 strprintf(&fileName, "%s/%s.serv", m_storeSettings.GetServicesDir().c_str(), name.c_str());
1586 if (Touch(fileName))
1588 std::lock_guard lock(m_mutex);
1589 m_errorStr = "Cannot create file " + fileName;
1590 printfd(__FILE__, "FILES_STORE::AddService - failed to add service '%s'\n", name.c_str());
1596 //-----------------------------------------------------------------------------*/
1597 int FILES_STORE::DelService(const std::string & name) const
1599 std::string fileName;
1600 strprintf(&fileName, "%s/%s.serv", m_storeSettings.GetServicesDir().c_str(), name.c_str());
1601 if (unlink(fileName.c_str()))
1603 std::lock_guard lock(m_mutex);
1604 m_errorStr = "unlink failed. Message: '";
1605 m_errorStr += strerror(errno);
1607 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1611 //-----------------------------------------------------------------------------*/
1612 int FILES_STORE::SaveService(const STG::ServiceConf & conf) const
1614 std::string fileName;
1616 strprintf(&fileName, "%s/%s.serv", m_storeSettings.GetServicesDir().c_str(), conf.name.c_str());
1619 CONFIGFILE cf(fileName, true);
1625 std::lock_guard lock(m_mutex);
1626 m_errorStr = "Cannot write service " + conf.name + ". " + fileName;
1627 printfd(__FILE__, "FILES_STORE::SaveService - failed to save service '%s'\n", conf.name.c_str());
1631 cf.WriteString("name", conf.name);
1632 cf.WriteString("comment", conf.comment);
1633 cf.WriteDouble("cost", conf.cost);
1634 cf.WriteInt("pay_day", conf.payDay);
1639 //-----------------------------------------------------------------------------
1640 int FILES_STORE::RestoreService(STG::ServiceConf * conf, const std::string & name) const
1642 std::string fileName;
1643 strprintf(&fileName, "%s/%s.serv", m_storeSettings.GetServicesDir().c_str(), name.c_str());
1644 CONFIGFILE cf(fileName);
1648 std::lock_guard lock(m_mutex);
1649 m_errorStr = "Cannot open " + fileName;
1650 printfd(__FILE__, "FILES_STORE::RestoreService - failed to restore service '%s'\n", name.c_str());
1654 if (cf.ReadString("name", &conf->name, name))
1656 std::lock_guard lock(m_mutex);
1657 m_errorStr = "Error in parameter 'name'";
1658 printfd(__FILE__, "FILES_STORE::RestoreService - name read failed for service '%s'\n", name.c_str());
1662 if (cf.ReadString("comment", &conf->comment, ""))
1664 std::lock_guard lock(m_mutex);
1665 m_errorStr = "Error in parameter 'comment'";
1666 printfd(__FILE__, "FILES_STORE::RestoreService - comment read failed for service '%s'\n", name.c_str());
1670 if (cf.ReadDouble("cost", &conf->cost, 0.0))
1672 std::lock_guard lock(m_mutex);
1673 m_errorStr = "Error in parameter 'cost'";
1674 printfd(__FILE__, "FILES_STORE::RestoreService - cost read failed for service '%s'\n", name.c_str());
1678 unsigned short value = 0;
1679 if (cf.ReadUShortInt("pay_day", &value, 0))
1681 std::lock_guard lock(m_mutex);
1682 m_errorStr = "Error in parameter 'pay_day'";
1683 printfd(__FILE__, "FILES_STORE::RestoreService - pay day read failed for service '%s'\n", name.c_str());
1686 conf->payDay = value;
1690 //-----------------------------------------------------------------------------
1691 int FILES_STORE::WriteDetailedStat(const STG::TraffStat & statTree,
1693 const std::string & login) const
1695 char fn[FN_STR_LEN];
1696 char dn[FN_STR_LEN];
1703 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", m_storeSettings.GetUsersDir().c_str(), login.c_str());
1704 if (access(dn, F_OK) != 0)
1706 if (mkdir(dn, 0700) != 0)
1708 std::lock_guard lock(m_mutex);
1709 m_errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1710 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1715 int e = chown(dn, m_storeSettings.GetStatUID(), m_storeSettings.GetStatGID());
1716 e += chmod(dn, m_storeSettings.GetStatModeDir());
1720 std::lock_guard lock(m_mutex);
1721 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1726 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1732 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1733 m_storeSettings.GetUsersDir().c_str(),
1737 if (access(dn, F_OK) != 0)
1739 if (mkdir(dn, 0700) != 0)
1741 std::lock_guard lock(m_mutex);
1742 m_errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1743 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1748 e = chown(dn, m_storeSettings.GetStatUID(), m_storeSettings.GetStatGID());
1749 e += chmod(dn, m_storeSettings.GetStatModeDir());
1753 std::lock_guard lock(m_mutex);
1754 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1757 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1758 m_storeSettings.GetUsersDir().c_str(),
1761 lt->tm_mon+1 < 10 ? "0" : "",
1763 if (access(dn, F_OK) != 0)
1765 if (mkdir(dn, 0700) != 0)
1767 std::lock_guard lock(m_mutex);
1768 m_errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1769 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1774 e = chown(dn, m_storeSettings.GetStatUID(), m_storeSettings.GetStatGID());
1775 e += chmod(dn, m_storeSettings.GetStatModeDir());
1779 std::lock_guard lock(m_mutex);
1780 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1783 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1785 statFile = fopen (fn, "at");
1789 std::lock_guard lock(m_mutex);
1790 m_errorStr = "File \'" + std::string(fn) + "\' cannot be written.";
1791 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1798 lt1 = localtime(&lastStat);
1807 lt2 = localtime(&t);
1813 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1814 h1, m1, s1, h2, m2, s2) < 0)
1816 std::lock_guard lock(m_mutex);
1817 m_errorStr = std::string("fprint failed. Message: '") + strerror(errno) + "'";
1818 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1823 auto stIter = statTree.begin();
1825 while (stIter != statTree.end())
1827 const auto u = std::to_string(stIter->second.up);
1828 const auto d = std::to_string(stIter->second.down);
1829 #ifdef TRAFF_STAT_WITH_PORTS
1830 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1831 inet_ntostring(stIter->first.ip).c_str(),
1836 stIter->second.cash) < 0)
1838 std::lock_guard lock(m_mutex);
1839 m_errorStr = "fprint failed. Message: '";
1840 m_errorStr += strerror(errno);
1842 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1847 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1848 inet_ntostring(stIter->first.ip).c_str(),
1852 stIter->second.cash) < 0)
1854 std::lock_guard lock(m_mutex);
1855 m_errorStr = std::string("fprint failed. Message: '");
1856 m_errorStr += strerror(errno);
1858 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1869 e = chown(fn, m_storeSettings.GetStatUID(), m_storeSettings.GetStatGID());
1870 e += chmod(fn, m_storeSettings.GetStatMode());
1874 std::lock_guard lock(m_mutex);
1875 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1880 //-----------------------------------------------------------------------------
1881 int FILES_STORE::AddMessage(STG::Message * msg, const std::string & login) const
1887 strprintf(&dn, "%s/%s/messages", m_storeSettings.GetUsersDir().c_str(), login.c_str());
1888 if (access(dn.c_str(), F_OK) != 0)
1890 if (mkdir(dn.c_str(), 0700) != 0)
1892 std::lock_guard lock(m_mutex);
1893 m_errorStr = "Directory \'";
1895 m_errorStr += "\' cannot be created.";
1896 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1901 chmod(dn.c_str(), m_storeSettings.GetConfModeDir());
1903 gettimeofday(&tv, NULL);
1905 msg->header.id = tv.tv_sec * 1000000 + tv.tv_usec;
1906 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1910 std::lock_guard lock(m_mutex);
1911 m_errorStr = "File \'";
1913 m_errorStr += "\' cannot be writen.";
1914 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1918 return EditMessage(*msg, login);
1920 //-----------------------------------------------------------------------------
1921 int FILES_STORE::EditMessage(const STG::Message & msg, const std::string & login) const
1923 std::string fileName;
1926 strprintf(&fileName, "%s/%s/messages/%lld", m_storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1928 if (access(fileName.c_str(), F_OK) != 0)
1930 std::lock_guard lock(m_mutex);
1931 m_errorStr = "Message for user \'";
1932 m_errorStr += login + "\' with ID \'";
1933 m_errorStr += std::to_string(msg.header.id) + "\' does not exist.";
1934 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", m_errorStr.c_str());
1938 Touch(fileName + ".new");
1940 msgFile = fopen((fileName + ".new").c_str(), "wt");
1943 std::lock_guard lock(m_mutex);
1944 m_errorStr = "File \'" + fileName + "\' cannot be writen.";
1945 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1950 res &= (fprintf(msgFile, "%u\n", msg.header.type) >= 0);
1951 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1952 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1953 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1954 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1955 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1956 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1960 std::lock_guard lock(m_mutex);
1961 m_errorStr = std::string("fprintf failed. Message: '") + strerror(errno) + "'";
1962 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
1969 chmod((fileName + ".new").c_str(), m_storeSettings.GetConfMode());
1971 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1973 std::lock_guard lock(m_mutex);
1974 m_errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1975 printfd(__FILE__, "FILES_STORE::EditMessage - rename failed. Message: '%s'\n", strerror(errno));
1981 //-----------------------------------------------------------------------------
1982 int FILES_STORE::GetMessage(uint64_t id, STG::Message * msg, const std::string & login) const
1985 strprintf(&fn, "%s/%s/messages/%lld", m_storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1986 msg->header.id = id;
1987 return ReadMessage(fn, &msg->header, &msg->text);
1989 //-----------------------------------------------------------------------------
1990 int FILES_STORE::DelMessage(uint64_t id, const std::string & login) const
1993 strprintf(&fn, "%s/%s/messages/%lld", m_storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1995 return unlink(fn.c_str());
1997 //-----------------------------------------------------------------------------
1998 int FILES_STORE::GetMessageHdrs(std::vector<STG::Message::Header> * hdrsList, const std::string & login) const
2000 std::string dn(m_storeSettings.GetUsersDir() + "/" + login + "/messages/");
2002 if (access(dn.c_str(), F_OK) != 0)
2007 std::vector<std::string> messages;
2008 GetFileList(&messages, dn, S_IFREG, "");
2010 for (unsigned i = 0; i < messages.size(); i++)
2012 unsigned long long id = 0;
2014 if (str2x(messages[i].c_str(), id))
2016 if (unlink((dn + messages[i]).c_str()))
2018 std::lock_guard lock(m_mutex);
2019 m_errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'";
2020 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2026 STG::Message::Header hdr;
2027 if (ReadMessage(dn + messages[i], &hdr, NULL))
2034 if (unlink((dn + messages[i]).c_str()))
2036 std::lock_guard lock(m_mutex);
2037 m_errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'";
2038 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2045 hdrsList->push_back(hdr);
2049 //-----------------------------------------------------------------------------
2050 int FILES_STORE::ReadMessage(const std::string & fileName,
2051 STG::Message::Header * hdr,
2052 std::string * text) const
2055 msgFile = fopen(fileName.c_str(), "rt");
2058 std::lock_guard lock(m_mutex);
2059 m_errorStr = "File \'";
2060 m_errorStr += fileName;
2061 m_errorStr += "\' cannot be openned.";
2062 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
2068 d[1] = &hdr->lastSendTime;
2069 d[2] = &hdr->creationTime;
2070 d[3] = &hdr->showTime;
2071 d[4] = reinterpret_cast<unsigned*>(&hdr->repeat);
2072 d[5] = &hdr->repeatPeriod;
2074 memset(p, 0, sizeof(p));
2076 for (int pos = 0; pos < 6; pos++)
2078 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
2079 std::lock_guard lock(m_mutex);
2080 m_errorStr = "Cannot read file \'";
2081 m_errorStr += fileName;
2082 m_errorStr += "\'. Missing data.";
2083 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
2084 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2090 ep = strrchr(p, '\r');
2092 ep = strrchr(p, '\n');
2097 std::lock_guard lock(m_mutex);
2098 m_errorStr = "Cannot read file \'";
2099 m_errorStr += fileName;
2100 m_errorStr += "\'. Missing data.";
2101 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
2102 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2107 if (str2x(p, *(d[pos])))
2109 std::lock_guard lock(m_mutex);
2110 m_errorStr = "Cannot read file \'";
2111 m_errorStr += fileName;
2112 m_errorStr += "\'. Incorrect value. \'";
2115 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
2122 memset(txt, 0, sizeof(txt));
2125 text->erase(text->begin(), text->end());
2126 while (!feof(msgFile))
2129 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2139 //-----------------------------------------------------------------------------
2140 int FILES_STORE::Touch(const std::string & path) const
2142 FILE * f = fopen(path.c_str(), "wb");
2150 //-----------------------------------------------------------------------------
2151 int GetFileList(std::vector<std::string> * fileList, const std::string & directory, mode_t mode, const std::string & ext)
2153 DIR * d = opendir(directory.c_str());
2157 printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno));
2162 while ((entry = readdir(d)))
2164 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
2167 std::string str = directory + "/" + std::string(entry->d_name);
2170 if (stat(str.c_str(), &st))
2173 if (!(st.st_mode & mode)) // Filter by mode
2179 size_t d_nameLen = strlen(entry->d_name);
2180 if (d_nameLen <= ext.size())
2183 if (ext == entry->d_name + (d_nameLen - ext.size()))
2185 entry->d_name[d_nameLen - ext.size()] = 0;
2186 fileList->push_back(entry->d_name);
2191 fileList->push_back(entry->d_name);
2199 //-----------------------------------------------------------------------------