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;
72 extern "C" STORE * GetStore();
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
78 return fsc.GetPlugin();
80 //-----------------------------------------------------------------------------
81 FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
96 //-----------------------------------------------------------------------------
97 int FILES_STORE_SETTINGS::ParseOwner(const std::vector<PARAM_VALUE> & moduleParams, const std::string & owner, uid_t * uid)
101 std::vector<PARAM_VALUE>::const_iterator pvi;
102 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
103 if (pvi == moduleParams.end() || pvi->value.empty())
105 errorStr = "Parameter \'" + owner + "\' not found.";
106 printfd(__FILE__, "%s\n", errorStr.c_str());
109 if (User2UID(pvi->value[0].c_str(), uid) < 0)
111 errorStr = "Parameter \'" + owner + "\': Unknown user \'" + pvi->value[0] + "\'";
112 printfd(__FILE__, "%s\n", errorStr.c_str());
117 //-----------------------------------------------------------------------------
118 int FILES_STORE_SETTINGS::ParseGroup(const std::vector<PARAM_VALUE> & moduleParams, const std::string & group, gid_t * gid)
122 std::vector<PARAM_VALUE>::const_iterator pvi;
123 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
124 if (pvi == moduleParams.end() || pvi->value.empty())
126 errorStr = "Parameter \'" + group + "\' not found.";
127 printfd(__FILE__, "%s\n", errorStr.c_str());
130 if (Group2GID(pvi->value[0].c_str(), gid) < 0)
132 errorStr = "Parameter \'" + group + "\': Unknown group \'" + pvi->value[0] + "\'";
133 printfd(__FILE__, "%s\n", errorStr.c_str());
138 //-----------------------------------------------------------------------------
139 int FILES_STORE_SETTINGS::ParseYesNo(const std::string & value, bool * val)
141 if (0 == strcasecmp(value.c_str(), "yes"))
146 if (0 == strcasecmp(value.c_str(), "no"))
152 errorStr = "Incorrect value \'" + value + "\'.";
155 //-----------------------------------------------------------------------------
156 int FILES_STORE_SETTINGS::ParseMode(const std::vector<PARAM_VALUE> & moduleParams, const std::string & modeStr, mode_t * mode)
160 std::vector<PARAM_VALUE>::const_iterator pvi;
161 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
162 if (pvi == moduleParams.end() || pvi->value.empty())
164 errorStr = "Parameter \'" + modeStr + "\' not found.";
165 printfd(__FILE__, "%s\n", errorStr.c_str());
168 if (Str2Mode(pvi->value[0].c_str(), mode) < 0)
170 errorStr = "Parameter \'" + modeStr + "\': Incorrect mode \'" + pvi->value[0] + "\'";
171 printfd(__FILE__, "%s\n", errorStr.c_str());
176 //-----------------------------------------------------------------------------
177 int FILES_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
179 if (ParseOwner(s.moduleParams, "StatOwner", &statUID) < 0)
181 if (ParseGroup(s.moduleParams, "StatGroup", &statGID) < 0)
183 if (ParseMode(s.moduleParams, "StatMode", &statMode) < 0)
186 if (ParseOwner(s.moduleParams, "ConfOwner", &confUID) < 0)
188 if (ParseGroup(s.moduleParams, "ConfGroup", &confGID) < 0)
190 if (ParseMode(s.moduleParams, "ConfMode", &confMode) < 0)
193 if (ParseOwner(s.moduleParams, "UserLogOwner", &userLogUID) < 0)
195 if (ParseGroup(s.moduleParams, "UserLogGroup", &userLogGID) < 0)
197 if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0)
200 std::vector<PARAM_VALUE>::const_iterator pvi;
202 pv.param = "RemoveBak";
203 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
204 if (pvi == s.moduleParams.end() || pvi->value.empty())
210 if (ParseYesNo(pvi->value[0], &removeBak))
212 printfd(__FILE__, "Cannot parse parameter 'RemoveBak'\n");
217 pv.param = "ReadBak";
218 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
219 if (pvi == s.moduleParams.end() || pvi->value.empty())
225 if (ParseYesNo(pvi->value[0], &readBak))
227 printfd(__FILE__, "Cannot parse parameter 'ReadBak'\n");
232 pv.param = "WorkDir";
233 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
234 if (pvi == s.moduleParams.end() || pvi->value.empty())
236 errorStr = "Parameter \'WorkDir\' not found.";
237 printfd(__FILE__, "Parameter 'WorkDir' not found\n");
241 workDir = pvi->value[0];
242 if (workDir.size() && workDir[workDir.size() - 1] == '/')
244 workDir.resize(workDir.size() - 1);
246 usersDir = workDir + "/users/";
247 tariffsDir = workDir + "/tariffs/";
248 adminsDir = workDir + "/admins/";
252 //-----------------------------------------------------------------------------
253 const std::string & FILES_STORE_SETTINGS::GetStrError() const
257 //-----------------------------------------------------------------------------
258 int FILES_STORE_SETTINGS::User2UID(const char * user, uid_t * uid)
264 errorStr = std::string("User \'") + std::string(user) + std::string("\' not found in system.");
265 printfd(__FILE__, "%s\n", errorStr.c_str());
272 //-----------------------------------------------------------------------------
273 int FILES_STORE_SETTINGS::Group2GID(const char * gr, gid_t * gid)
279 errorStr = std::string("Group \'") + std::string(gr) + std::string("\' not found in system.");
280 printfd(__FILE__, "%s\n", errorStr.c_str());
287 //-----------------------------------------------------------------------------
288 int FILES_STORE_SETTINGS::Str2Mode(const char * str, mode_t * mode)
295 errorStr = std::string("Error parsing mode \'") + str + std::string("\'");
296 printfd(__FILE__, "%s\n", errorStr.c_str());
300 for (int i = 0; i < 3; i++)
301 if (str[i] > '7' || str[i] < '0')
303 errorStr = std::string("Error parsing mode \'") + str + std::string("\'");
304 printfd(__FILE__, "%s\n", errorStr.c_str());
312 *mode = ((mode_t)c) + ((mode_t)b << 3) + ((mode_t)a << 6);
316 //-----------------------------------------------------------------------------
317 mode_t FILES_STORE_SETTINGS::GetStatModeDir() const
319 mode_t mode = statMode;
320 if (statMode & S_IRUSR) mode |= S_IXUSR;
321 if (statMode & S_IRGRP) mode |= S_IXGRP;
322 if (statMode & S_IROTH) mode |= S_IXOTH;
325 //-----------------------------------------------------------------------------
326 mode_t FILES_STORE_SETTINGS::GetConfModeDir() const
328 mode_t mode = confMode;
329 if (confMode & S_IRUSR) mode |= S_IXUSR;
330 if (confMode & S_IRGRP) mode |= S_IXGRP;
331 if (confMode & S_IROTH) mode |= S_IXOTH;
334 //-----------------------------------------------------------------------------
335 //-----------------------------------------------------------------------------
336 //-----------------------------------------------------------------------------
337 FILES_STORE::FILES_STORE()
339 version("file_store v.1.04"),
343 logger(GetPluginLogger(GetStgLogger(), "store_files"))
345 pthread_mutexattr_t attr;
346 pthread_mutexattr_init(&attr);
347 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
348 pthread_mutex_init(&mutex, &attr);
350 //-----------------------------------------------------------------------------
351 int FILES_STORE::ParseSettings()
353 int ret = storeSettings.ParseSettings(settings);
356 STG_LOCKER lock(&mutex);
357 errorStr = storeSettings.GetStrError();
361 //-----------------------------------------------------------------------------
362 int FILES_STORE::GetUsersList(std::vector<std::string> * userList) const
364 std::vector<std::string> files;
366 if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, ""))
368 STG_LOCKER lock(&mutex);
369 errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + std::string(strerror(errno));
373 STG_LOCKER lock(&mutex);
375 userList->swap(files);
379 //-----------------------------------------------------------------------------
380 int FILES_STORE::GetAdminsList(std::vector<std::string> * adminList) const
382 std::vector<std::string> files;
384 if (GetFileList(&files, storeSettings.GetAdminsDir(), S_IFREG, ".adm"))
386 STG_LOCKER lock(&mutex);
387 errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + std::string(strerror(errno));
391 STG_LOCKER lock(&mutex);
393 adminList->swap(files);
397 //-----------------------------------------------------------------------------
398 int FILES_STORE::GetTariffsList(std::vector<std::string> * tariffList) const
400 std::vector<std::string> files;
402 if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf"))
404 STG_LOCKER lock(&mutex);
405 errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + std::string(strerror(errno));
409 STG_LOCKER lock(&mutex);
411 tariffList->swap(files);
415 //-----------------------------------------------------------------------------
416 int FILES_STORE::RemoveDir(const char * path) const
418 DIR * d = opendir(path);
422 errorStr = "failed to open dir. Message: '";
423 errorStr += strerror(errno);
425 printfd(__FILE__, "FILE_STORE::RemoveDir() - Failed to open dir '%s': '%s'\n", path, strerror(errno));
430 while ((entry = readdir(d)))
432 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
435 std::string str = path;
436 str += "/" + std::string(entry->d_name);
439 if (stat(str.c_str(), &st))
442 if ((st.st_mode & S_IFREG))
444 if (unlink(str.c_str()))
446 STG_LOCKER lock(&mutex);
447 errorStr = "unlink failed. Message: '";
448 errorStr += strerror(errno);
450 printfd(__FILE__, "FILES_STORE::RemoveDir() - unlink failed. Message: '%s'\n", strerror(errno));
456 if (!(st.st_mode & S_IFDIR))
458 if (RemoveDir(str.c_str()))
471 STG_LOCKER lock(&mutex);
472 errorStr = "rmdir failed. Message: '";
473 errorStr += strerror(errno);
475 printfd(__FILE__, "FILES_STORE::RemoveDir() - rmdir failed. Message: '%s'\n", strerror(errno));
481 //-----------------------------------------------------------------------------
482 int FILES_STORE::AddUser(const std::string & login) const
484 std::string fileName;
486 strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str());
488 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
490 STG_LOCKER lock(&mutex);
491 errorStr = std::string("mkdir failed. Message: '") + strerror(errno) + "'";
492 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
496 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
499 STG_LOCKER lock(&mutex);
500 errorStr = "Cannot create file \"" + fileName + "\'";
501 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
505 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
508 STG_LOCKER lock(&mutex);
509 errorStr = "Cannot create file \"" + fileName + "\'";
510 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
515 //-----------------------------------------------------------------------------
516 int FILES_STORE::DelUser(const std::string & login) const
519 std::string dirName1;
521 strprintf(&dirName, "%s/%s", storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR);
522 if (access(dirName.c_str(), F_OK) != 0)
524 if (mkdir(dirName.c_str(), 0700) != 0)
526 STG_LOCKER lock(&mutex);
527 errorStr = "Directory '" + dirName + "' cannot be created.";
528 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
533 if (access(dirName.c_str(), F_OK) == 0)
535 strprintf(&dirName, "%s/%s/%s.%lu", storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR, login.c_str(), time(NULL));
536 strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
537 if (rename(dirName1.c_str(), dirName.c_str()))
539 STG_LOCKER lock(&mutex);
540 errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
541 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
547 strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
548 if (RemoveDir(dirName.c_str()))
555 //-----------------------------------------------------------------------------
556 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const
558 std::string fileName;
559 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
560 if (RestoreUserConf(conf, login, fileName))
562 if (!storeSettings.GetReadBak())
566 return RestoreUserConf(conf, login, fileName + ".bak");
570 //-----------------------------------------------------------------------------
571 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login, const std::string & fileName) const
573 CONFIGFILE cf(fileName);
578 STG_LOCKER lock(&mutex);
579 errorStr = "User \'" + login + "\' data not read.";
580 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
584 if (cf.ReadString("Password", &conf->password, "") < 0)
586 STG_LOCKER lock(&mutex);
587 errorStr = "User \'" + login + "\' data not read. Parameter Password.";
588 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
591 if (conf->password.empty())
593 STG_LOCKER lock(&mutex);
594 errorStr = "User \'" + login + "\' password is blank.";
595 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
599 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
601 STG_LOCKER lock(&mutex);
602 errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
603 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
606 if (conf->tariffName.empty())
608 STG_LOCKER lock(&mutex);
609 errorStr = "User \'" + login + "\' tariff is blank.";
610 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
615 cf.ReadString("IP", &ipStr, "?");
619 ips = StrToIPS(ipStr);
621 catch (const std::string & s)
623 STG_LOCKER lock(&mutex);
624 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
625 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
630 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
632 STG_LOCKER lock(&mutex);
633 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
634 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
638 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
640 STG_LOCKER lock(&mutex);
641 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
642 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
646 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
648 STG_LOCKER lock(&mutex);
649 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
650 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
654 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
655 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
656 cf.ReadString("TariffChange", &conf->nextTariff, "");
657 cf.ReadString("Group", &conf->group, "");
658 cf.ReadString("RealName", &conf->realName, "");
659 cf.ReadString("Address", &conf->address, "");
660 cf.ReadString("Phone", &conf->phone, "");
661 cf.ReadString("Note", &conf->note, "");
662 cf.ReadString("email", &conf->email, "");
664 char userdataName[12];
665 for (int i = 0; i < USERDATA_NUM; i++)
667 snprintf(userdataName, 12, "Userdata%d", i);
668 cf.ReadString(userdataName, &conf->userdata[i], "");
671 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
673 STG_LOCKER lock(&mutex);
674 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
675 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
681 //-----------------------------------------------------------------------------
682 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const
684 std::string fileName;
685 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
687 if (RestoreUserStat(stat, login, fileName))
689 if (!storeSettings.GetReadBak())
693 return RestoreUserStat(stat, login, fileName + ".bak");
697 //-----------------------------------------------------------------------------
698 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login, const std::string & fileName) const
700 CONFIGFILE cf(fileName);
706 STG_LOCKER lock(&mutex);
707 errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
708 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
714 for (int i = 0; i < DIR_NUM; i++)
717 snprintf(s, 22, "D%d", i);
718 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
720 STG_LOCKER lock(&mutex);
721 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
722 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
725 stat->monthDown[i] = traff;
727 snprintf(s, 22, "U%d", i);
728 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
730 STG_LOCKER lock(&mutex);
731 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
732 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
735 stat->monthUp[i] = traff;
738 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
740 STG_LOCKER lock(&mutex);
741 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
742 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
746 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
748 STG_LOCKER lock(&mutex);
749 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
750 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
754 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
756 STG_LOCKER lock(&mutex);
757 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
758 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
762 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
764 STG_LOCKER lock(&mutex);
765 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
766 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
770 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
772 STG_LOCKER lock(&mutex);
773 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
774 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
778 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
780 STG_LOCKER lock(&mutex);
781 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
782 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
788 //-----------------------------------------------------------------------------
789 int FILES_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const
791 std::string fileName;
792 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
794 CONFIGFILE cfstat(fileName, true);
796 int e = cfstat.Error();
800 STG_LOCKER lock(&mutex);
801 errorStr = std::string("User \'") + login + "\' conf not written\n";
802 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
806 e = chmod(fileName.c_str(), storeSettings.GetConfMode());
807 e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID());
811 STG_LOCKER lock(&mutex);
812 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
815 cfstat.WriteString("Password", conf.password);
816 cfstat.WriteInt ("Passive", conf.passive);
817 cfstat.WriteInt ("Down", conf.disabled);
818 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
819 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
820 cfstat.WriteString("Tariff", conf.tariffName);
821 cfstat.WriteString("Address", conf.address);
822 cfstat.WriteString("Phone", conf.phone);
823 cfstat.WriteString("Email", conf.email);
824 cfstat.WriteString("Note", conf.note);
825 cfstat.WriteString("RealName", conf.realName);
826 cfstat.WriteString("Group", conf.group);
827 cfstat.WriteDouble("Credit", conf.credit);
828 cfstat.WriteString("TariffChange", conf.nextTariff);
830 char userdataName[12];
831 for (int i = 0; i < USERDATA_NUM; i++)
833 snprintf(userdataName, 12, "Userdata%d", i);
834 cfstat.WriteString(userdataName, conf.userdata[i]);
836 cfstat.WriteInt("CreditExpire", conf.creditExpire);
838 std::ostringstream ipStr;
840 cfstat.WriteString("IP", ipStr.str());
844 //-----------------------------------------------------------------------------
845 int FILES_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
847 std::string fileName;
848 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
851 CONFIGFILE cfstat(fileName, true);
852 int e = cfstat.Error();
856 STG_LOCKER lock(&mutex);
857 errorStr = std::string("User \'") + login + "\' stat not written\n";
858 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
862 for (int i = 0; i < DIR_NUM; i++)
865 snprintf(s, 22, "D%d", i);
866 cfstat.WriteInt(s, stat.monthDown[i]);
867 snprintf(s, 22, "U%d", i);
868 cfstat.WriteInt(s, stat.monthUp[i]);
871 cfstat.WriteDouble("Cash", stat.cash);
872 cfstat.WriteDouble("FreeMb", stat.freeMb);
873 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
874 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
875 cfstat.WriteInt("PassiveTime", stat.passiveTime);
876 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
879 int e = chmod(fileName.c_str(), storeSettings.GetStatMode());
880 e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID());
884 STG_LOCKER lock(&mutex);
885 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
890 //-----------------------------------------------------------------------------
891 int FILES_STORE::WriteLogString(const std::string & str, const std::string & login) const
894 time_t tm = time(NULL);
895 std::string fileName;
896 fileName = storeSettings.GetUsersDir() + "/" + login + "/log";
897 f = fopen(fileName.c_str(), "at");
901 fprintf(f, "%s", LogDate(tm));
903 fprintf(f, "%s", str.c_str());
909 STG_LOCKER lock(&mutex);
910 errorStr = "Cannot open \'" + fileName + "\'";
911 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
915 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
916 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
920 STG_LOCKER lock(&mutex);
921 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
926 //-----------------------------------------------------------------------------
927 int FILES_STORE::WriteLog2String(const std::string & str, const std::string & login) const
930 time_t tm = time(NULL);
931 std::string fileName;
932 fileName = storeSettings.GetUsersDir() + "/" + login + "/log2";
933 f = fopen(fileName.c_str(), "at");
937 fprintf(f, "%s", LogDate(tm));
939 fprintf(f, "%s", str.c_str());
945 STG_LOCKER lock(&mutex);
946 errorStr = "Cannot open \'" + fileName + "\'";
947 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
951 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
952 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
956 STG_LOCKER lock(&mutex);
957 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
962 //-----------------------------------------------------------------------------
963 int FILES_STORE::WriteUserChgLog(const std::string & login,
964 const std::string & admLogin,
966 const std::string & paramName,
967 const std::string & oldValue,
968 const std::string & newValue,
969 const std::string & message) const
971 std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
972 + paramName + "\' parameter changed from \'" + oldValue +
973 "\' to \'" + newValue + "\'. " + message;
975 return WriteLogString(userLogMsg, login);
977 //-----------------------------------------------------------------------------
978 int FILES_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
980 std::string logStr = "Connect, " + inet_ntostring(ip);
981 if (WriteLogString(logStr, login))
983 return WriteLog2String(logStr, login);
985 //-----------------------------------------------------------------------------
986 int FILES_STORE::WriteUserDisconnect(const std::string & login,
987 const DIR_TRAFF & monthUp,
988 const DIR_TRAFF & monthDown,
989 const DIR_TRAFF & sessionUp,
990 const DIR_TRAFF & sessionDown,
993 const std::string & reason) const
995 std::ostringstream logStr;
996 logStr << "Disconnect, "
997 << " session upload: \'"
999 << "\' session download: \'"
1001 << "\' month upload: \'"
1003 << "\' month download: \'"
1009 if (WriteLogString(logStr.str(), login))
1012 logStr << " freeMb: \'"
1019 return WriteLog2String(logStr.str(), login);
1021 //-----------------------------------------------------------------------------
1022 int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const std::string & login) const
1026 strprintf(&stat1,"%s/%s/stat.%d.%02d",
1027 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1029 CONFIGFILE s(stat1, true);
1033 STG_LOCKER lock(&mutex);
1034 errorStr = "Cannot create file '" + stat1 + "'";
1035 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1041 strprintf(&stat2,"%s/%s/stat2.%d.%02d",
1042 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1044 CONFIGFILE s2(stat2, true);
1048 STG_LOCKER lock(&mutex);
1049 errorStr = "Cannot create file '" + stat2 + "'";
1050 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1054 for (size_t i = 0; i < DIR_NUM; i++)
1057 snprintf(dirName, 3, "U%llu", (unsigned long long)i);
1058 s.WriteInt(dirName, stat.monthUp[i]); // Classic
1059 s2.WriteInt(dirName, stat.monthUp[i]); // New
1060 snprintf(dirName, 3, "D%llu", (unsigned long long)i);
1061 s.WriteInt(dirName, stat.monthDown[i]); // Classic
1062 s2.WriteInt(dirName, stat.monthDown[i]); // New
1066 s.WriteDouble("cash", stat.cash);
1069 s2.WriteDouble("Cash", stat.cash);
1070 s2.WriteDouble("FreeMb", stat.freeMb);
1071 s2.WriteDouble("LastCashAdd", stat.lastCashAdd);
1072 s2.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1073 s2.WriteInt("PassiveTime", stat.passiveTime);
1074 s2.WriteInt("LastActivityTime", stat.lastActivityTime);
1078 //-----------------------------------------------------------------------------*/
1079 int FILES_STORE::AddAdmin(const std::string & login) const
1081 std::string fileName;
1082 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1084 if (Touch(fileName))
1086 STG_LOCKER lock(&mutex);
1087 errorStr = "Cannot create file " + fileName;
1088 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1094 //-----------------------------------------------------------------------------*/
1095 int FILES_STORE::DelAdmin(const std::string & login) const
1097 std::string fileName;
1098 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1099 if (unlink(fileName.c_str()))
1101 STG_LOCKER lock(&mutex);
1102 errorStr = "unlink failed. Message: '";
1103 errorStr += strerror(errno);
1105 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1109 //-----------------------------------------------------------------------------*/
1110 int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1112 std::string fileName;
1114 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1117 CONFIGFILE cf(fileName, true);
1123 STG_LOCKER lock(&mutex);
1124 errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1125 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1129 char pass[ADM_PASSWD_LEN + 1];
1130 memset(pass, 0, sizeof(pass));
1132 char adminPass[ADM_PASSWD_LEN + 1];
1133 memset(adminPass, 0, sizeof(adminPass));
1136 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1138 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1139 adminPass[ADM_PASSWD_LEN - 1] = 0;
1141 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1143 EncryptBlock(pass + 8*i, adminPass + 8*i, &ctx);
1146 pass[ADM_PASSWD_LEN - 1] = 0;
1147 char passwordE[2 * ADM_PASSWD_LEN + 2];
1148 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1150 cf.WriteString("password", passwordE);
1151 cf.WriteInt("ChgConf", ac.priv.userConf);
1152 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1153 cf.WriteInt("ChgStat", ac.priv.userStat);
1154 cf.WriteInt("ChgCash", ac.priv.userCash);
1155 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1156 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1157 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1158 cf.WriteInt("ChgService", ac.priv.serviceChg);
1159 cf.WriteInt("ChgCorp", ac.priv.corpChg);
1164 //-----------------------------------------------------------------------------
1165 int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const
1167 std::string fileName;
1168 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1169 CONFIGFILE cf(fileName);
1170 char pass[ADM_PASSWD_LEN + 1];
1171 char password[ADM_PASSWD_LEN + 1];
1172 char passwordE[2 * ADM_PASSWD_LEN + 2];
1179 STG_LOCKER lock(&mutex);
1180 errorStr = "Cannot open " + fileName;
1181 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1185 if (cf.ReadString("password", &p, "*"))
1187 STG_LOCKER lock(&mutex);
1188 errorStr = "Error in parameter password";
1189 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1193 memset(passwordE, 0, sizeof(passwordE));
1194 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1196 memset(pass, 0, sizeof(pass));
1198 if (passwordE[0] != 0)
1200 Decode21(pass, passwordE);
1201 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1203 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1205 DecryptBlock(password + 8*i, pass + 8*i, &ctx);
1213 ac->password = password;
1217 if (cf.ReadUShortInt("ChgConf", &a, 0) == 0)
1218 ac->priv.userConf = a;
1221 STG_LOCKER lock(&mutex);
1222 errorStr = "Error in parameter ChgConf";
1223 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1227 if (cf.ReadUShortInt("ChgPassword", &a, 0) == 0)
1228 ac->priv.userPasswd = a;
1231 STG_LOCKER lock(&mutex);
1232 errorStr = "Error in parameter ChgPassword";
1233 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1237 if (cf.ReadUShortInt("ChgStat", &a, 0) == 0)
1238 ac->priv.userStat = a;
1241 STG_LOCKER lock(&mutex);
1242 errorStr = "Error in parameter ChgStat";
1243 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1247 if (cf.ReadUShortInt("ChgCash", &a, 0) == 0)
1248 ac->priv.userCash = a;
1251 STG_LOCKER lock(&mutex);
1252 errorStr = "Error in parameter ChgCash";
1253 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1257 if (cf.ReadUShortInt("UsrAddDel", &a, 0) == 0)
1258 ac->priv.userAddDel = a;
1261 STG_LOCKER lock(&mutex);
1262 errorStr = "Error in parameter UsrAddDel";
1263 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1267 if (cf.ReadUShortInt("ChgAdmin", &a, 0) == 0)
1268 ac->priv.adminChg = a;
1271 STG_LOCKER lock(&mutex);
1272 errorStr = "Error in parameter ChgAdmin";
1273 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1277 if (cf.ReadUShortInt("ChgTariff", &a, 0) == 0)
1278 ac->priv.tariffChg = a;
1281 STG_LOCKER lock(&mutex);
1282 errorStr = "Error in parameter ChgTariff";
1283 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1287 if (cf.ReadUShortInt("ChgService", &a, 0) == 0)
1288 ac->priv.serviceChg = a;
1290 ac->priv.serviceChg = 0;
1292 if (cf.ReadUShortInt("ChgCorp", &a, 0) == 0)
1293 ac->priv.corpChg = a;
1295 ac->priv.corpChg = 0;
1299 //-----------------------------------------------------------------------------
1300 int FILES_STORE::AddTariff(const std::string & name) const
1302 std::string fileName;
1303 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1304 if (Touch(fileName))
1306 STG_LOCKER lock(&mutex);
1307 errorStr = "Cannot create file " + fileName;
1308 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1313 //-----------------------------------------------------------------------------
1314 int FILES_STORE::DelTariff(const std::string & name) const
1316 std::string fileName;
1317 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1318 if (unlink(fileName.c_str()))
1320 STG_LOCKER lock(&mutex);
1321 errorStr = "unlink failed. Message: '";
1322 errorStr += strerror(errno);
1324 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1328 //-----------------------------------------------------------------------------
1329 int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const
1331 std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1332 CONFIGFILE conf(fileName);
1334 td->tariffConf.name = tariffName;
1336 if (conf.Error() != 0)
1338 STG_LOCKER lock(&mutex);
1339 errorStr = "Cannot read file " + fileName;
1340 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1345 for (int i = 0; i<DIR_NUM; i++)
1347 strprintf(¶m, "Time%d", i);
1348 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1350 STG_LOCKER lock(&mutex);
1351 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1352 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1356 ParseTariffTimeStr(str.c_str(),
1357 td->dirPrice[i].hDay,
1358 td->dirPrice[i].mDay,
1359 td->dirPrice[i].hNight,
1360 td->dirPrice[i].mNight);
1362 strprintf(¶m, "PriceDayA%d", i);
1363 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1365 STG_LOCKER lock(&mutex);
1366 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1367 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1370 td->dirPrice[i].priceDayA /= (1024*1024);
1372 strprintf(¶m, "PriceDayB%d", i);
1373 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1375 STG_LOCKER lock(&mutex);
1376 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1377 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1380 td->dirPrice[i].priceDayB /= (1024*1024);
1382 strprintf(¶m, "PriceNightA%d", i);
1383 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1385 STG_LOCKER lock(&mutex);
1386 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1387 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1390 td->dirPrice[i].priceNightA /= (1024*1024);
1392 strprintf(¶m, "PriceNightB%d", i);
1393 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1395 STG_LOCKER lock(&mutex);
1396 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1397 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1400 td->dirPrice[i].priceNightB /= (1024*1024);
1402 strprintf(¶m, "Threshold%d", i);
1403 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1405 STG_LOCKER lock(&mutex);
1406 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1407 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1411 strprintf(¶m, "SinglePrice%d", i);
1412 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1414 STG_LOCKER lock(&mutex);
1415 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1416 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1420 strprintf(¶m, "NoDiscount%d", i);
1421 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1423 STG_LOCKER lock(&mutex);
1424 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1425 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1430 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1432 STG_LOCKER lock(&mutex);
1433 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1434 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1438 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1440 STG_LOCKER lock(&mutex);
1441 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1442 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1446 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1448 STG_LOCKER lock(&mutex);
1449 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1450 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1454 if (conf.ReadString("TraffType", &str, "") < 0)
1456 STG_LOCKER lock(&mutex);
1457 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1458 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1462 td->tariffConf.traffType = TARIFF::StringToTraffType(str);
1464 if (conf.ReadString("Period", &str, "month") < 0)
1465 td->tariffConf.period = TARIFF::MONTH;
1467 td->tariffConf.period = TARIFF::StringToPeriod(str);
1469 if (conf.ReadString("ChangePolicy", &str, "allow") < 0)
1470 td->tariffConf.changePolicy = TARIFF::ALLOW;
1472 td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(str);
1474 if (conf.ReadTime("ChangePolicyTimeout", &td->tariffConf.changePolicyTimeout, 0) < 0)
1476 STG_LOCKER lock(&mutex);
1477 errorStr = "Cannot read tariff " + tariffName + ". Parameter ChangePolicyTimeout";
1478 printfd(__FILE__, "FILES_STORE::RestoreTariff - changepolicytimeout read failed for tariff '%s'\n", tariffName.c_str());
1483 //-----------------------------------------------------------------------------
1484 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const
1486 std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1489 CONFIGFILE cf(fileName, true);
1495 STG_LOCKER lock(&mutex);
1496 errorStr = "Error writing tariff " + tariffName;
1497 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1502 for (int i = 0; i < DIR_NUM; i++)
1504 strprintf(¶m, "PriceDayA%d", i);
1505 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1507 strprintf(¶m, "PriceDayB%d", i);
1508 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1510 strprintf(¶m, "PriceNightA%d", i);
1511 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1513 strprintf(¶m, "PriceNightB%d", i);
1514 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1516 strprintf(¶m, "Threshold%d", i);
1517 cf.WriteInt(param, td.dirPrice[i].threshold);
1520 strprintf(¶m, "Time%d", i);
1522 strprintf(&s, "%0d:%0d-%0d:%0d",
1523 td.dirPrice[i].hDay,
1524 td.dirPrice[i].mDay,
1525 td.dirPrice[i].hNight,
1526 td.dirPrice[i].mNight);
1528 cf.WriteString(param, s);
1530 strprintf(¶m, "NoDiscount%d", i);
1531 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1533 strprintf(¶m, "SinglePrice%d", i);
1534 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1537 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1538 cf.WriteDouble("Fee", td.tariffConf.fee);
1539 cf.WriteDouble("Free", td.tariffConf.free);
1540 cf.WriteString("TraffType", TARIFF::TraffTypeToString(td.tariffConf.traffType));
1541 cf.WriteString("Period", TARIFF::PeriodToString(td.tariffConf.period));
1542 cf.WriteString("ChangePolicy", TARIFF::ChangePolicyToString(td.tariffConf.changePolicy));
1547 //-----------------------------------------------------------------------------
1548 int FILES_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree,
1550 const std::string & login) const
1552 char fn[FN_STR_LEN];
1553 char dn[FN_STR_LEN];
1560 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1561 if (access(dn, F_OK) != 0)
1563 if (mkdir(dn, 0700) != 0)
1565 STG_LOCKER lock(&mutex);
1566 errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1567 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1572 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1573 e += chmod(dn, storeSettings.GetStatModeDir());
1577 STG_LOCKER lock(&mutex);
1578 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1583 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1589 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1590 storeSettings.GetUsersDir().c_str(),
1594 if (access(dn, F_OK) != 0)
1596 if (mkdir(dn, 0700) != 0)
1598 STG_LOCKER lock(&mutex);
1599 errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1600 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1605 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1606 e += chmod(dn, storeSettings.GetStatModeDir());
1610 STG_LOCKER lock(&mutex);
1611 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1614 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1615 storeSettings.GetUsersDir().c_str(),
1618 lt->tm_mon+1 < 10 ? "0" : "",
1620 if (access(dn, F_OK) != 0)
1622 if (mkdir(dn, 0700) != 0)
1624 STG_LOCKER lock(&mutex);
1625 errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1626 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1631 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1632 e += chmod(dn, storeSettings.GetStatModeDir());
1636 STG_LOCKER lock(&mutex);
1637 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1640 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1642 statFile = fopen (fn, "at");
1646 STG_LOCKER lock(&mutex);
1647 errorStr = "File \'" + std::string(fn) + "\' cannot be written.";
1648 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1655 lt1 = localtime(&lastStat);
1664 lt2 = localtime(&t);
1670 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1671 h1, m1, s1, h2, m2, s2) < 0)
1673 STG_LOCKER lock(&mutex);
1674 errorStr = std::string("fprint failed. Message: '") + strerror(errno) + "'";
1675 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1680 std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1681 stIter = statTree.begin();
1683 while (stIter != statTree.end())
1686 x2str(stIter->second.up, u);
1687 x2str(stIter->second.down, d);
1688 #ifdef TRAFF_STAT_WITH_PORTS
1689 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1690 inet_ntostring(stIter->first.ip).c_str(),
1695 stIter->second.cash) < 0)
1697 STG_LOCKER lock(&mutex);
1698 errorStr = "fprint failed. Message: '";
1699 errorStr += strerror(errno);
1701 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1706 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1707 inet_ntostring(stIter->first.ip).c_str(),
1711 stIter->second.cash) < 0)
1713 STG_LOCKER lock(&mutex);
1714 errorStr = std::string("fprint failed. Message: '");
1715 errorStr += strerror(errno);
1717 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1728 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1729 e += chmod(fn, storeSettings.GetStatMode());
1733 STG_LOCKER lock(&mutex);
1734 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1739 //-----------------------------------------------------------------------------
1740 int FILES_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
1746 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1747 if (access(dn.c_str(), F_OK) != 0)
1749 if (mkdir(dn.c_str(), 0700) != 0)
1751 STG_LOCKER lock(&mutex);
1752 errorStr = "Directory \'";
1754 errorStr += "\' cannot be created.";
1755 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1760 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1762 gettimeofday(&tv, NULL);
1764 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1765 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1769 STG_LOCKER lock(&mutex);
1770 errorStr = "File \'";
1772 errorStr += "\' cannot be writen.";
1773 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1777 return EditMessage(*msg, login);
1779 //-----------------------------------------------------------------------------
1780 int FILES_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const
1782 std::string fileName;
1785 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1787 if (access(fileName.c_str(), F_OK) != 0)
1790 x2str(msg.header.id, idstr);
1791 STG_LOCKER lock(&mutex);
1792 errorStr = "Message for user \'";
1793 errorStr += login + "\' with ID \'";
1794 errorStr += idstr + "\' does not exist.";
1795 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
1799 Touch(fileName + ".new");
1801 msgFile = fopen((fileName + ".new").c_str(), "wt");
1804 STG_LOCKER lock(&mutex);
1805 errorStr = "File \'" + fileName + "\' cannot be writen.";
1806 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1811 res &= (fprintf(msgFile, "%u\n", msg.header.type) >= 0);
1812 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1813 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1814 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1815 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1816 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1817 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1821 STG_LOCKER lock(&mutex);
1822 errorStr = std::string("fprintf failed. Message: '") + strerror(errno) + "'";
1823 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
1830 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
1832 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1834 STG_LOCKER lock(&mutex);
1835 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1836 printfd(__FILE__, "FILES_STORE::EditMessage - rename failed. Message: '%s'\n", strerror(errno));
1842 //-----------------------------------------------------------------------------
1843 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const
1846 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1847 msg->header.id = id;
1848 return ReadMessage(fn, &msg->header, &msg->text);
1850 //-----------------------------------------------------------------------------
1851 int FILES_STORE::DelMessage(uint64_t id, const std::string & login) const
1854 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1856 return unlink(fn.c_str());
1858 //-----------------------------------------------------------------------------
1859 int FILES_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const
1861 std::string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/");
1863 if (access(dn.c_str(), F_OK) != 0)
1868 std::vector<std::string> messages;
1869 GetFileList(&messages, dn, S_IFREG, "");
1871 for (unsigned i = 0; i < messages.size(); i++)
1873 unsigned long long id = 0;
1875 if (str2x(messages[i].c_str(), id))
1877 if (unlink((dn + messages[i]).c_str()))
1879 STG_LOCKER lock(&mutex);
1880 errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'";
1881 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
1888 if (ReadMessage(dn + messages[i], &hdr, NULL))
1895 if (unlink((dn + messages[i]).c_str()))
1897 STG_LOCKER lock(&mutex);
1898 errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'";
1899 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
1906 hdrsList->push_back(hdr);
1910 //-----------------------------------------------------------------------------
1911 int FILES_STORE::ReadMessage(const std::string & fileName,
1913 std::string * text) const
1916 msgFile = fopen(fileName.c_str(), "rt");
1919 STG_LOCKER lock(&mutex);
1920 errorStr = "File \'";
1921 errorStr += fileName;
1922 errorStr += "\' cannot be openned.";
1923 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
1929 d[1] = &hdr->lastSendTime;
1930 d[2] = &hdr->creationTime;
1931 d[3] = &hdr->showTime;
1932 d[4] = (unsigned*)(&hdr->repeat);
1933 d[5] = &hdr->repeatPeriod;
1935 memset(p, 0, sizeof(p));
1937 for (int pos = 0; pos < 6; pos++)
1939 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
1940 STG_LOCKER lock(&mutex);
1941 errorStr = "Cannot read file \'";
1942 errorStr += fileName;
1943 errorStr += "\'. Missing data.";
1944 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
1945 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
1951 ep = strrchr(p, '\r');
1953 ep = strrchr(p, '\n');
1958 STG_LOCKER lock(&mutex);
1959 errorStr = "Cannot read file \'";
1960 errorStr += fileName;
1961 errorStr += "\'. Missing data.";
1962 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
1963 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
1968 if (str2x(p, *(d[pos])))
1970 STG_LOCKER lock(&mutex);
1971 errorStr = "Cannot read file \'";
1972 errorStr += fileName;
1973 errorStr += "\'. Incorrect value. \'";
1976 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
1983 memset(txt, 0, sizeof(txt));
1986 text->erase(text->begin(), text->end());
1987 while (!feof(msgFile))
1990 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2000 //-----------------------------------------------------------------------------
2001 int FILES_STORE::Touch(const std::string & path) const
2003 FILE * f = fopen(path.c_str(), "wb");
2011 //-----------------------------------------------------------------------------
2012 int GetFileList(std::vector<std::string> * fileList, const std::string & directory, mode_t mode, const std::string & ext)
2014 DIR * d = opendir(directory.c_str());
2018 printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno));
2023 while ((entry = readdir(d)))
2025 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
2028 std::string str = directory + "/" + std::string(entry->d_name);
2031 if (stat(str.c_str(), &st))
2034 if (!(st.st_mode & mode)) // Filter by mode
2040 size_t d_nameLen = strlen(entry->d_name);
2041 if (d_nameLen <= ext.size())
2044 if (ext == entry->d_name + (d_nameLen - ext.size()))
2046 entry->d_name[d_nameLen - ext.size()] = 0;
2047 fileList->push_back(entry->d_name);
2052 fileList->push_back(entry->d_name);
2060 //-----------------------------------------------------------------------------