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 $
33 #include <sys/types.h>
49 #include "user_conf.h"
50 #include "user_stat.h"
51 #include "stg_const.h"
52 #include "file_store.h"
54 #include "stg_logger.h"
55 #include "stg_locker.h"
57 #define DELETED_USERS_DIR "deleted_users"
59 #define adm_enc_passwd "cjeifY8m3"
63 const int pt_mega = 1024 * 1024;
64 //-----------------------------------------------------------------------------
69 //-------------------------------------------------------------------------
70 BAK_FILE(const string & fileName, bool removeBak)
75 BAK_FILE::removeBak = removeBak;
76 fileNameBak = fileName + ".bak";
77 if (rename(fileName.c_str(), fileNameBak.c_str()))
79 printfd(__FILE__, "BAK_FILE::BAK_FILE - rename failed. Message: '%s'\n", strerror(errno));
87 //-------------------------------------------------------------------------
90 if(bakSuccessed && removeBak)
92 if (unlink(fileNameBak.c_str()))
94 printfd(__FILE__, "BAK_FILE::~BAK_FILE - unlink failed. Message: '%s'\n", strerror(errno));
98 //-------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
107 class FILES_STORE_CREATOR
113 FILES_STORE_CREATOR()
114 : fs(new FILES_STORE())
117 ~FILES_STORE_CREATOR()
122 FILES_STORE * GetStore()
127 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
130 FILES_STORE_CREATOR fsc;
131 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
133 //-----------------------------------------------------------------------------
134 BASE_STORE * GetStore()
136 return fsc.GetStore();
138 //-----------------------------------------------------------------------------
139 FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
145 //-----------------------------------------------------------------------------
146 FILES_STORE_SETTINGS::~FILES_STORE_SETTINGS()
149 //-----------------------------------------------------------------------------
150 int FILES_STORE_SETTINGS::ParseOwner(const vector<PARAM_VALUE> & moduleParams, const string & owner, uid_t * uid)
154 vector<PARAM_VALUE>::const_iterator pvi;
155 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
156 if (pvi == moduleParams.end())
158 errorStr = "Parameter \'" + owner + "\' not found.";
159 printfd(__FILE__, "%s\n", errorStr.c_str());
162 if (User2UID(pvi->value[0].c_str(), uid) < 0)
164 errorStr = "Parameter \'" + owner + "\': Unknown user \'" + pvi->value[0] + "\'";
165 printfd(__FILE__, "%s\n", errorStr.c_str());
170 //-----------------------------------------------------------------------------
171 int FILES_STORE_SETTINGS::ParseGroup(const vector<PARAM_VALUE> & moduleParams, const string & group, gid_t * gid)
175 vector<PARAM_VALUE>::const_iterator pvi;
176 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
177 if (pvi == moduleParams.end())
179 errorStr = "Parameter \'" + group + "\' not found.";
180 printfd(__FILE__, "%s\n", errorStr.c_str());
183 if (Group2GID(pvi->value[0].c_str(), gid) < 0)
185 errorStr = "Parameter \'" + group + "\': Unknown group \'" + pvi->value[0] + "\'";
186 printfd(__FILE__, "%s\n", errorStr.c_str());
191 //-----------------------------------------------------------------------------
192 int FILES_STORE_SETTINGS::ParseYesNo(const string & value, bool * val)
194 if (0 == strcasecmp(value.c_str(), "yes"))
199 if (0 == strcasecmp(value.c_str(), "no"))
205 errorStr = "Incorrect value \'" + value + "\'.";
208 //-----------------------------------------------------------------------------
209 int FILES_STORE_SETTINGS::ParseMode(const vector<PARAM_VALUE> & moduleParams, const string & modeStr, mode_t * mode)
213 vector<PARAM_VALUE>::const_iterator pvi;
214 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
215 if (pvi == moduleParams.end())
217 errorStr = "Parameter \'" + modeStr + "\' not found.";
218 printfd(__FILE__, "%s\n", errorStr.c_str());
221 if (Str2Mode(pvi->value[0].c_str(), mode) < 0)
223 errorStr = "Parameter \'" + modeStr + "\': Incorrect mode \'" + pvi->value[0] + "\'";
224 printfd(__FILE__, "%s\n", errorStr.c_str());
229 //-----------------------------------------------------------------------------
230 int FILES_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
232 if (ParseOwner(s.moduleParams, "StatOwner", &statUID) < 0)
234 if (ParseGroup(s.moduleParams, "StatGroup", &statGID) < 0)
236 if (ParseMode(s.moduleParams, "StatMode", &statMode) < 0)
239 if (ParseOwner(s.moduleParams, "ConfOwner", &confUID) < 0)
241 if (ParseGroup(s.moduleParams, "ConfGroup", &confGID) < 0)
243 if (ParseMode(s.moduleParams, "ConfMode", &confMode) < 0)
246 if (ParseOwner(s.moduleParams, "UserLogOwner", &userLogUID) < 0)
248 if (ParseGroup(s.moduleParams, "UserLogGroup", &userLogGID) < 0)
250 if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0)
253 vector<PARAM_VALUE>::const_iterator pvi;
255 pv.param = "RemoveBak";
256 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
257 if (pvi == s.moduleParams.end())
263 if (ParseYesNo(pvi->value[0], &removeBak))
265 printfd(__FILE__, "Cannot parse parameter 'RemoveBak'\n");
270 pv.param = "ReadBak";
271 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
272 if (pvi == s.moduleParams.end())
278 if (ParseYesNo(pvi->value[0], &readBak))
280 printfd(__FILE__, "Cannot parse parameter 'ReadBak'\n");
285 pv.param = "WorkDir";
286 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
287 if (pvi == s.moduleParams.end())
289 errorStr = "Parameter \'WorkDir\' not found.";
290 printfd(__FILE__, "Parameter 'WorkDir' not found\n");
294 workDir = pvi->value[0];
295 if (workDir.size() && workDir[workDir.size() - 1] == '/')
297 workDir.resize(workDir.size() - 1);
299 usersDir = workDir + "/users/";
300 tariffsDir = workDir + "/tariffs/";
301 adminsDir = workDir + "/admins/";
305 //-----------------------------------------------------------------------------
306 const string & FILES_STORE_SETTINGS::GetStrError() const
310 //-----------------------------------------------------------------------------
311 int FILES_STORE_SETTINGS::User2UID(const char * user, uid_t * uid)
317 errorStr = string("User \'") + string(user) + string("\' not found in system.");
318 printfd(__FILE__, "%s\n", errorStr.c_str());
325 //-----------------------------------------------------------------------------
326 int FILES_STORE_SETTINGS::Group2GID(const char * gr, gid_t * gid)
332 errorStr = string("Group \'") + string(gr) + string("\' not found in system.");
333 printfd(__FILE__, "%s\n", errorStr.c_str());
340 //-----------------------------------------------------------------------------
341 int FILES_STORE_SETTINGS::Str2Mode(const char * str, mode_t * mode)
348 errorStr = string("Error parsing mode \'") + str + string("\'");
349 printfd(__FILE__, "%s\n", errorStr.c_str());
353 for (int i = 0; i < 3; i++)
354 if (str[i] > '7' || str[i] < '0')
356 errorStr = string("Error parsing mode \'") + str + string("\'");
357 printfd(__FILE__, "%s\n", errorStr.c_str());
365 *mode = ((mode_t)c) + ((mode_t)b << 3) + ((mode_t)a << 6);
369 //-----------------------------------------------------------------------------
370 string FILES_STORE_SETTINGS::GetWorkDir() const
374 //-----------------------------------------------------------------------------
375 string FILES_STORE_SETTINGS::GetUsersDir() const
379 //-----------------------------------------------------------------------------
380 string FILES_STORE_SETTINGS::GetAdminsDir() const
384 //-----------------------------------------------------------------------------
385 string FILES_STORE_SETTINGS::GetTariffsDir() const
389 //-----------------------------------------------------------------------------
390 mode_t FILES_STORE_SETTINGS::GetStatMode() const
394 //-----------------------------------------------------------------------------
395 mode_t FILES_STORE_SETTINGS::GetStatModeDir() const
397 mode_t mode = statMode;
398 if (statMode & S_IRUSR) mode |= S_IXUSR;
399 if (statMode & S_IRGRP) mode |= S_IXGRP;
400 if (statMode & S_IROTH) mode |= S_IXOTH;
403 //-----------------------------------------------------------------------------
404 uid_t FILES_STORE_SETTINGS::GetStatUID() const
408 //-----------------------------------------------------------------------------
409 gid_t FILES_STORE_SETTINGS::GetStatGID() const
413 //-----------------------------------------------------------------------------
414 mode_t FILES_STORE_SETTINGS::GetConfMode() const
418 //-----------------------------------------------------------------------------
419 mode_t FILES_STORE_SETTINGS::GetConfModeDir() const
421 mode_t mode = confMode;
422 if (confMode & S_IRUSR) mode |= S_IXUSR;
423 if (confMode & S_IRGRP) mode |= S_IXGRP;
424 if (confMode & S_IROTH) mode |= S_IXOTH;
427 //-----------------------------------------------------------------------------
428 uid_t FILES_STORE_SETTINGS::GetConfUID() const
432 //-----------------------------------------------------------------------------
433 gid_t FILES_STORE_SETTINGS::GetConfGID() const
437 //-----------------------------------------------------------------------------
438 mode_t FILES_STORE_SETTINGS::GetLogMode() const
442 //-----------------------------------------------------------------------------
443 uid_t FILES_STORE_SETTINGS::GetLogUID() const
447 //-----------------------------------------------------------------------------
448 gid_t FILES_STORE_SETTINGS::GetLogGID() const
452 //-----------------------------------------------------------------------------
453 bool FILES_STORE_SETTINGS::GetRemoveBak() const
457 //-----------------------------------------------------------------------------
458 bool FILES_STORE_SETTINGS::GetReadBak() const
462 //-----------------------------------------------------------------------------
463 //-----------------------------------------------------------------------------
464 //-----------------------------------------------------------------------------
465 FILES_STORE::FILES_STORE()
467 version = "file_store v.1.04";
469 pthread_mutexattr_t attr;
470 pthread_mutexattr_init(&attr);
471 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
472 pthread_mutex_init(&mutex, &attr);
474 //-----------------------------------------------------------------------------
475 FILES_STORE::~FILES_STORE()
479 //-----------------------------------------------------------------------------
480 void FILES_STORE::SetSettings(const MODULE_SETTINGS & s)
484 //-----------------------------------------------------------------------------
485 int FILES_STORE::ParseSettings()
487 int ret = storeSettings.ParseSettings(settings);
490 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
491 errorStr = storeSettings.GetStrError();
495 //-----------------------------------------------------------------------------
496 const string & FILES_STORE::GetStrError() const
498 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
501 //-----------------------------------------------------------------------------
502 const string & FILES_STORE::GetVersion() const
506 //-----------------------------------------------------------------------------
507 int FILES_STORE::GetFilesList(vector<string> * filesList, const string & directory, mode_t mode, const string & ext) const
509 // æÕÎËÃÉÑ ÐÒÏÓÍÁÔÒÉ×ÁÅÔ ÓÏÄÅÒÖÉÍÏÅ ÄÉÒÅËÔÏÒÉÉ
517 d = opendir(directory.c_str());
521 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
522 errorStr = "Directory \'" + directory + "\' cannot be opened.";
523 //printfd(__FILE__, "%s\n", errorStr.c_str());
528 int extLen = ext.size() ;
529 while ((dir = readdir(d)))
531 if (strcmp(dir->d_name, ".") && strcmp(dir->d_name, ".."))
533 str = directory + "/" + string(dir->d_name);
534 if (!stat(str.c_str(), &st))
536 if (st.st_mode & mode) // ïÔÓÅ× ÆÁÊÌÏ× or directories
538 d_nameLen = strlen(dir->d_name);
539 if (d_nameLen > extLen)
541 if (strcmp(dir->d_name + (d_nameLen - extLen), ext.c_str()) == 0)
543 dir->d_name[d_nameLen - extLen] = 0;
544 filesList->push_back(dir->d_name);
555 //-----------------------------------------------------------------------------
556 int FILES_STORE::GetUsersList(vector<string> * usersList) const
558 return GetFilesList(usersList, storeSettings.GetUsersDir(), S_IFDIR, "");
560 //-----------------------------------------------------------------------------
561 int FILES_STORE::GetAdminsList(vector<string> * adminsList) const
563 return GetFilesList(adminsList, storeSettings.GetAdminsDir(), S_IFREG, ".adm");
565 //-----------------------------------------------------------------------------
566 int FILES_STORE::GetTariffsList(vector<string> * tariffsList) const
568 return GetFilesList(tariffsList, storeSettings.GetTariffsDir(), S_IFREG, ".tf");
570 //-----------------------------------------------------------------------------
571 int FILES_STORE::RemoveDir(const char * path) const
573 vector<string> filesList;
575 GetFilesList(&filesList, path, S_IFREG, "");
577 for (unsigned i = 0; i < filesList.size(); i++)
579 string file = path + string("/") + filesList[i];
580 if (unlink(file.c_str()))
582 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
583 errorStr = "unlink failed. Message: '";
584 errorStr += strerror(errno);
586 printfd(__FILE__, "FILES_STORE::RemoveDir - unlink failed. Message: '%s'\n", strerror(errno));
591 GetFilesList(&filesList, path, S_IFDIR, "");
593 for (unsigned i = 0; i < filesList.size(); i++)
595 string dir = string(path) + "/" + filesList[i];
596 RemoveDir(dir.c_str());
601 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
602 errorStr = "rmdir failed. Message: '";
603 errorStr += strerror(errno);
605 printfd(__FILE__, "FILES_STORE::RemoveDir - rmdir failed. Message: '%s'\n", strerror(errno));
611 //-----------------------------------------------------------------------------
612 int FILES_STORE::AddUser(const string & login) const
616 strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str());
618 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
620 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
621 errorStr = string("mkdir failed. Message: '") + strerror(errno) + "'";
622 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
626 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
629 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
630 errorStr = "Cannot create file \"" + fileName + "\'";
631 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
634 /*f = fopen(fileName.c_str(), "wt");
637 if (fprintf(f, "\n") < 0)
639 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
640 errorStr = "fprintf failed. Message: '";
641 errorStr += strerror(errno);
643 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
650 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
651 errorStr = "Cannot create file \"" + fileName + "\'";
652 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
656 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
659 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
660 errorStr = "Cannot create file \"" + fileName + "\'";
661 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
664 /*f = fopen(fileName.c_str(), "wt");
667 if (fprintf(f, "\n") < 0)
669 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
670 errorStr = "fprintf failed. Message: '";
671 errorStr += strerror(errno);
673 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
680 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
681 errorStr = "Cannot create file \"" + fileName + "\'";
682 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
687 //-----------------------------------------------------------------------------
688 int FILES_STORE::DelUser(const string & login) const
693 strprintf(&dirName, "%s/"DELETED_USERS_DIR, storeSettings.GetWorkDir().c_str());
694 if (access(dirName.c_str(), F_OK) != 0)
696 if (mkdir(dirName.c_str(), 0700) != 0)
698 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
699 errorStr = "Directory '" + dirName + "' cannot be created.";
700 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
705 if (access(dirName.c_str(), F_OK) == 0)
707 strprintf(&dirName, "%s/"DELETED_USERS_DIR"/%s.%lu", storeSettings.GetWorkDir().c_str(), login.c_str(), time(NULL));
708 strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
709 if (rename(dirName1.c_str(), dirName.c_str()))
711 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
712 errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
713 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
719 strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
720 if (RemoveDir(dirName.c_str()))
727 //-----------------------------------------------------------------------------
728 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
731 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
732 if (RestoreUserConf(conf, login, fileName))
734 if (!storeSettings.GetReadBak())
738 return RestoreUserConf(conf, login, fileName + ".bak");
742 //-----------------------------------------------------------------------------
743 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login, const string & fileName) const
745 CONFIGFILE cf(fileName);
751 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
752 errorStr = "User \'" + login + "\' data not read.";
753 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
757 if (cf.ReadString("Password", &conf->password, "") < 0)
759 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
760 errorStr = "User \'" + login + "\' data not read. Parameter Password.";
761 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
764 if (conf->password.empty())
766 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
767 errorStr = "User \'" + login + "\' password is blank.";
768 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
772 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
774 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
775 errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
776 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
779 if (conf->tariffName.empty())
781 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
782 errorStr = "User \'" + login + "\' tariff is blank.";
783 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
788 cf.ReadString("IP", &ipStr, "?");
796 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
797 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
798 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
803 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
805 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
806 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
807 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
811 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
813 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
814 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
815 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
819 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
821 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
822 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
823 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
827 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
828 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
829 cf.ReadString("TariffChange", &conf->nextTariff, "");
830 cf.ReadString("Group", &conf->group, "");
831 cf.ReadString("RealName", &conf->realName, "");
832 cf.ReadString("Address", &conf->address, "");
833 cf.ReadString("Phone", &conf->phone, "");
834 cf.ReadString("Note", &conf->note, "");
835 cf.ReadString("email", &conf->email, "");
837 char userdataName[12];
838 for (int i = 0; i < USERDATA_NUM; i++)
840 snprintf(userdataName, 12, "Userdata%d", i);
841 cf.ReadString(userdataName, &conf->userdata[i], "");
844 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
846 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
847 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
848 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
854 //-----------------------------------------------------------------------------
855 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
858 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
860 if (RestoreUserStat(stat, login, fileName))
862 if (!storeSettings.GetReadBak())
866 return RestoreUserStat(stat, login, fileName + ".bak");
870 //-----------------------------------------------------------------------------
871 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login, const string & fileName) const
873 CONFIGFILE cf(fileName);
879 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
880 errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
881 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
887 for (int i = 0; i < DIR_NUM; i++)
890 snprintf(s, 22, "D%d", i);
891 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
893 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
894 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
895 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
898 stat->down[i] = traff;
900 snprintf(s, 22, "U%d", i);
901 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
903 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
904 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
905 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
911 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
913 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
914 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
915 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
919 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
921 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
922 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
923 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
927 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
929 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
930 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
931 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
935 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
937 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
938 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
939 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
943 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
945 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
946 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
947 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
951 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
953 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
954 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
955 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
961 //-----------------------------------------------------------------------------
962 int FILES_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
965 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
967 //BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak());
969 CONFIGFILE cfstat(fileName, true);
971 int e = cfstat.Error();
975 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
976 errorStr = string("User \'") + login + "\' conf not written\n";
977 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
981 e = chmod(fileName.c_str(), storeSettings.GetConfMode());
982 e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID());
986 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
987 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
990 cfstat.WriteString("Password", conf.password);
991 cfstat.WriteInt ("Passive", conf.passive);
992 cfstat.WriteInt ("Down", conf.disabled);
993 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
994 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
995 cfstat.WriteString("Tariff", conf.tariffName);
996 cfstat.WriteString("Address", conf.address);
997 cfstat.WriteString("Phone", conf.phone);
998 cfstat.WriteString("Email", conf.email);
999 cfstat.WriteString("Note", conf.note);
1000 cfstat.WriteString("RealName", conf.realName);
1001 cfstat.WriteString("Group", conf.group);
1002 cfstat.WriteDouble("Credit", conf.credit);
1003 cfstat.WriteString("TariffChange", conf.nextTariff);
1005 char userdataName[12];
1006 for (int i = 0; i < USERDATA_NUM; i++)
1008 snprintf(userdataName, 12, "Userdata%d", i);
1009 cfstat.WriteString(userdataName, conf.userdata[i]);
1011 cfstat.WriteInt("CreditExpire", conf.creditExpire);
1015 cfstat.WriteString("IP", ipStr.str());
1019 //-----------------------------------------------------------------------------
1020 int FILES_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
1024 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
1026 //BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak());
1029 CONFIGFILE cfstat(fileName, true);
1030 int e = cfstat.Error();
1034 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1035 errorStr = string("User \'") + login + "\' stat not written\n";
1036 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
1040 for (int i = 0; i < DIR_NUM; i++)
1042 snprintf(s, 22, "D%d", i);
1043 cfstat.WriteInt(s, stat.down[i]);
1044 snprintf(s, 22, "U%d", i);
1045 cfstat.WriteInt(s, stat.up[i]);
1048 cfstat.WriteDouble("Cash", stat.cash);
1049 cfstat.WriteDouble("FreeMb", stat.freeMb);
1050 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
1051 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1052 cfstat.WriteInt("PassiveTime", stat.passiveTime);
1053 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
1056 e = chmod(fileName.c_str(), storeSettings.GetStatMode());
1057 e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID());
1061 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1062 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1067 //-----------------------------------------------------------------------------
1068 int FILES_STORE::WriteLogString(const string & str, const string & login) const
1071 time_t tm = time(NULL);
1073 fileName = storeSettings.GetUsersDir() + "/" + login + "/log";
1074 f = fopen(fileName.c_str(), "at");
1078 fprintf(f, "%s", LogDate(tm));
1080 fprintf(f, "%s", str.c_str());
1086 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1087 errorStr = "Cannot open \'" + fileName + "\'";
1088 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1092 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1093 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1097 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1098 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1103 //-----------------------------------------------------------------------------
1104 int FILES_STORE::WriteLog2String(const string & str, const string & login) const
1107 time_t tm = time(NULL);
1109 fileName = storeSettings.GetUsersDir() + "/" + login + "/log2";
1110 f = fopen(fileName.c_str(), "at");
1114 fprintf(f, "%s", LogDate(tm));
1116 fprintf(f, "%s", str.c_str());
1122 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1123 errorStr = "Cannot open \'" + fileName + "\'";
1124 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1128 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1129 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1133 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1134 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1139 //-----------------------------------------------------------------------------
1140 int FILES_STORE::WriteUserChgLog(const string & login,
1141 const string & admLogin,
1143 const string & paramName,
1144 const string & oldValue,
1145 const string & newValue,
1146 const string & message) const
1148 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1149 + paramName + "\' parameter changed from \'" + oldValue +
1150 "\' to \'" + newValue + "\'. " + message;
1152 return WriteLogString(userLogMsg, login);
1154 //-----------------------------------------------------------------------------
1155 int FILES_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1157 string logStr = "Connect, " + inet_ntostring(ip);
1158 if (WriteLogString(logStr, login))
1160 return WriteLog2String(logStr, login);
1162 //-----------------------------------------------------------------------------
1163 int FILES_STORE::WriteUserDisconnect(const string & login,
1164 const DIR_TRAFF & up,
1165 const DIR_TRAFF & down,
1166 const DIR_TRAFF & sessionUp,
1167 const DIR_TRAFF & sessionDown,
1170 const std::string & reason) const
1172 stringstream logStr;
1173 logStr << "Disconnect, "
1174 << " session upload: \'"
1176 << "\' session download: \'"
1178 << "\' month upload: \'"
1180 << "\' month download: \'"
1186 if (WriteLogString(logStr.str(), login))
1189 logStr << " freeMb: \'"
1196 return WriteLog2String(logStr.str(), login);
1198 //-----------------------------------------------------------------------------
1199 int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const
1204 strprintf(&str,"%s/%s/stat.%d.%02d",
1205 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1207 CONFIGFILE s(str, true);
1212 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1213 errorStr = "Cannot create file " + str;
1214 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1220 for (int i = 0; i < DIR_NUM; i++)
1222 snprintf(dirName, 3, "U%d", i);
1223 s.WriteInt(dirName, stat.up[i]);
1224 snprintf(dirName, 3, "D%d", i);
1225 s.WriteInt(dirName, stat.down[i]);
1228 s.WriteDouble("cash", stat.cash);
1232 //-----------------------------------------------------------------------------*/
1233 int FILES_STORE::AddAdmin(const string & login) const
1236 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1238 if (Touch(fileName))
1240 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1241 errorStr = "Cannot create file " + fileName;
1242 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1248 //-----------------------------------------------------------------------------*/
1249 int FILES_STORE::DelAdmin(const string & login) const
1252 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1253 if (unlink(fileName.c_str()))
1255 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1256 errorStr = "unlink failed. Message: '";
1257 errorStr += strerror(errno);
1259 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1263 //-----------------------------------------------------------------------------*/
1264 int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1266 char passwordE[2 * ADM_PASSWD_LEN + 2];
1267 char pass[ADM_PASSWD_LEN + 1];
1268 char adminPass[ADM_PASSWD_LEN + 1];
1272 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1275 CONFIGFILE cf(fileName, true);
1281 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1282 errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1283 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1287 memset(pass, 0, sizeof(pass));
1288 memset(adminPass, 0, sizeof(adminPass));
1291 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1293 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1294 adminPass[ADM_PASSWD_LEN - 1] = 0;
1296 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1298 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1301 pass[ADM_PASSWD_LEN - 1] = 0;
1302 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1304 cf.WriteString("password", passwordE);
1305 cf.WriteInt("ChgConf", ac.priv.userConf);
1306 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1307 cf.WriteInt("ChgStat", ac.priv.userStat);
1308 cf.WriteInt("ChgCash", ac.priv.userCash);
1309 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1310 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1311 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1316 //-----------------------------------------------------------------------------
1317 int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1320 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1321 CONFIGFILE cf(fileName);
1322 char pass[ADM_PASSWD_LEN + 1];
1323 char password[ADM_PASSWD_LEN + 1];
1324 char passwordE[2 * ADM_PASSWD_LEN + 2];
1331 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1332 errorStr = "Cannot open " + fileName;
1333 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1339 if (cf.ReadString("password", &p, "*"))
1341 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1342 errorStr = "Error in parameter password";
1343 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1347 memset(passwordE, 0, sizeof(passwordE));
1348 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1350 //printfd(__FILE__, "passwordE %s\n", passwordE);
1352 memset(pass, 0, sizeof(pass));
1354 if (passwordE[0] != 0)
1356 Decode21(pass, passwordE);
1357 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1359 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1361 DecodeString(password + 8*i, pass + 8*i, &ctx);
1369 ac->password = password;
1371 if (cf.ReadInt("ChgConf", &a, 0) == 0)
1372 ac->priv.userConf = a;
1375 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1376 errorStr = "Error in parameter ChgConf";
1377 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1381 if (cf.ReadInt("ChgPassword", &a, 0) == 0)
1382 ac->priv.userPasswd = a;
1385 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1386 errorStr = "Error in parameter ChgPassword";
1387 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1391 if (cf.ReadInt("ChgStat", &a, 0) == 0)
1392 ac->priv.userStat = a;
1395 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1396 errorStr = "Error in parameter ChgStat";
1397 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1401 if (cf.ReadInt("ChgCash", &a, 0) == 0)
1402 ac->priv.userCash = a;
1405 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1406 errorStr = "Error in parameter ChgCash";
1407 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1411 if (cf.ReadInt("UsrAddDel", &a, 0) == 0)
1412 ac->priv.userAddDel = a;
1415 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1416 errorStr = "Error in parameter UsrAddDel";
1417 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1421 if (cf.ReadInt("ChgAdmin", &a, 0) == 0)
1422 ac->priv.adminChg = a;
1425 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1426 errorStr = "Error in parameter ChgAdmin";
1427 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1431 if (cf.ReadInt("ChgTariff", &a, 0) == 0)
1432 ac->priv.tariffChg = a;
1435 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1436 errorStr = "Error in parameter ChgTariff";
1437 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1443 //-----------------------------------------------------------------------------
1444 int FILES_STORE::AddTariff(const string & name) const
1447 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1448 if (Touch(fileName))
1450 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1451 errorStr = "Cannot create file " + fileName;
1452 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1457 //-----------------------------------------------------------------------------
1458 int FILES_STORE::DelTariff(const string & name) const
1461 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1462 if (unlink(fileName.c_str()))
1464 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1465 errorStr = "unlink failed. Message: '";
1466 errorStr += strerror(errno);
1468 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1472 //-----------------------------------------------------------------------------
1473 int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1475 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1476 CONFIGFILE conf(fileName);
1478 td->tariffConf.name = tariffName;
1480 if (conf.Error() != 0)
1482 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1483 errorStr = "Cannot read file " + fileName;
1484 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1489 for (int i = 0; i<DIR_NUM; i++)
1491 strprintf(¶m, "Time%d", i);
1492 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1494 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1495 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1496 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1500 ParseTariffTimeStr(str.c_str(),
1501 td->dirPrice[i].hDay,
1502 td->dirPrice[i].mDay,
1503 td->dirPrice[i].hNight,
1504 td->dirPrice[i].mNight);
1506 strprintf(¶m, "PriceDayA%d", i);
1507 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1509 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1510 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1511 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1514 td->dirPrice[i].priceDayA /= (1024*1024);
1516 strprintf(¶m, "PriceDayB%d", i);
1517 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1519 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1520 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1521 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1524 td->dirPrice[i].priceDayB /= (1024*1024);
1526 strprintf(¶m, "PriceNightA%d", i);
1527 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1529 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1530 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1531 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1534 td->dirPrice[i].priceNightA /= (1024*1024);
1536 strprintf(¶m, "PriceNightB%d", i);
1537 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1539 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1540 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1541 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1544 td->dirPrice[i].priceNightB /= (1024*1024);
1546 strprintf(¶m, "Threshold%d", i);
1547 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1549 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1550 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1551 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1555 strprintf(¶m, "SinglePrice%d", i);
1556 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1558 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1559 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1560 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1564 strprintf(¶m, "NoDiscount%d", i);
1565 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1567 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1568 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1569 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1574 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1576 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1577 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1578 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1582 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1584 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1585 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1586 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1590 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1592 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1593 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1594 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1598 if (conf.ReadString("TraffType", &str, "") < 0)
1600 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1601 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1602 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1606 if (!strcasecmp(str.c_str(), "up"))
1607 td->tariffConf.traffType = TRAFF_UP;
1609 if (!strcasecmp(str.c_str(), "down"))
1610 td->tariffConf.traffType = TRAFF_DOWN;
1612 if (!strcasecmp(str.c_str(), "up+down"))
1613 td->tariffConf.traffType = TRAFF_UP_DOWN;
1615 if (!strcasecmp(str.c_str(), "max"))
1616 td->tariffConf.traffType = TRAFF_MAX;
1619 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1620 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1621 printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str());
1626 //-----------------------------------------------------------------------------
1627 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1629 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1632 CONFIGFILE cf(fileName, true);
1638 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1639 errorStr = "Error writing tariff " + tariffName;
1640 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1645 for (int i = 0; i < DIR_NUM; i++)
1647 strprintf(¶m, "PriceDayA%d", i);
1648 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1650 strprintf(¶m, "PriceDayB%d", i);
1651 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1653 strprintf(¶m, "PriceNightA%d", i);
1654 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1656 strprintf(¶m, "PriceNightB%d", i);
1657 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1659 strprintf(¶m, "Threshold%d", i);
1660 cf.WriteInt(param, td.dirPrice[i].threshold);
1663 strprintf(¶m, "Time%d", i);
1665 strprintf(&s, "%0d:%0d-%0d:%0d",
1666 td.dirPrice[i].hDay,
1667 td.dirPrice[i].mDay,
1668 td.dirPrice[i].hNight,
1669 td.dirPrice[i].mNight);
1671 cf.WriteString(param, s);
1673 strprintf(¶m, "NoDiscount%d", i);
1674 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1676 strprintf(¶m, "SinglePrice%d", i);
1677 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1680 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1681 cf.WriteDouble("Fee", td.tariffConf.fee);
1682 cf.WriteDouble("Free", td.tariffConf.free);
1684 switch (td.tariffConf.traffType)
1687 cf.WriteString("TraffType", "up");
1690 cf.WriteString("TraffType", "down");
1693 cf.WriteString("TraffType", "up+down");
1696 cf.WriteString("TraffType", "max");
1703 //-----------------------------------------------------------------------------
1704 int FILES_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1706 const string & login) const
1708 char fn[FN_STR_LEN];
1709 char dn[FN_STR_LEN];
1716 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1717 if (access(dn, F_OK) != 0)
1719 if (mkdir(dn, 0700) != 0)
1721 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1722 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1723 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1728 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1729 e += chmod(dn, storeSettings.GetStatModeDir());
1733 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1734 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1739 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1745 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1746 storeSettings.GetUsersDir().c_str(),
1750 if (access(dn, F_OK) != 0)
1752 if (mkdir(dn, 0700) != 0)
1754 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1755 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1756 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1761 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1762 e += chmod(dn, storeSettings.GetStatModeDir());
1766 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1767 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1770 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1771 storeSettings.GetUsersDir().c_str(),
1774 lt->tm_mon+1 < 10 ? "0" : "",
1776 if (access(dn, F_OK) != 0)
1778 if (mkdir(dn, 0700) != 0)
1780 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1781 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1782 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1787 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1788 e += chmod(dn, storeSettings.GetStatModeDir());
1792 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1793 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1796 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1798 statFile = fopen (fn, "at");
1802 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1803 errorStr = "File \'" + string(fn) + "\' cannot be written.";
1804 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1811 lt1 = localtime(&lastStat);
1820 lt2 = localtime(&t);
1826 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1827 h1, m1, s1, h2, m2, s2) < 0)
1829 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1830 errorStr = string("fprint failed. Message: '") + strerror(errno) + "'";
1831 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1836 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1837 stIter = statTree.begin();
1839 while (stIter != statTree.end())
1842 x2str(stIter->second.up, u);
1843 x2str(stIter->second.down, d);
1844 #ifdef TRAFF_STAT_WITH_PORTS
1845 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1846 inet_ntostring(stIter->first.ip).c_str(),
1851 stIter->second.cash) < 0)
1853 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1854 errorStr = "fprint failed. Message: '";
1855 errorStr += strerror(errno);
1857 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1862 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1863 inet_ntostring(stIter->first.ip).c_str(),
1867 stIter->second.cash) < 0)
1869 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1870 errorStr = string("fprint failed. Message: '");
1871 errorStr += strerror(errno);
1873 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1884 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1885 e += chmod(fn, storeSettings.GetStatMode());
1889 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1890 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1895 //-----------------------------------------------------------------------------
1896 int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const
1898 //ðÒÏ×ÅÒÉÔØ ÅÓÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1899 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1900 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1906 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1907 if (access(dn.c_str(), F_OK) != 0)
1909 if (mkdir(dn.c_str(), 0700) != 0)
1911 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1912 errorStr = "Directory \'";
1914 errorStr += "\' cannot be created.";
1915 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1920 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1922 gettimeofday(&tv, NULL);
1924 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1925 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1929 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1930 errorStr = "File \'";
1932 errorStr += "\' cannot be writen.";
1933 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1937 return EditMessage(*msg, login);
1939 //-----------------------------------------------------------------------------
1940 int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1942 //ðÒÏ×ÅÒÉÔØ ÅÓÌÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1943 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1944 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1949 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1951 if (access(fileName.c_str(), F_OK) != 0)
1954 x2str(msg.header.id, idstr);
1955 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1956 errorStr = "Message for user \'";
1957 errorStr += login + "\' with ID \'";
1958 errorStr += idstr + "\' does not exist.";
1959 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
1963 Touch(fileName + ".new");
1965 msgFile = fopen((fileName + ".new").c_str(), "wt");
1968 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1969 errorStr = "File \'" + fileName + "\' cannot be writen.";
1970 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1975 res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0);
1976 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1977 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1978 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1979 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1980 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1981 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1985 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1986 errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'";
1987 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
1993 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
1995 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1997 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1998 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1999 printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno));
2005 //-----------------------------------------------------------------------------
2006 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
2009 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2010 msg->header.id = id;
2011 return ReadMessage(fn, &msg->header, &msg->text);
2013 //-----------------------------------------------------------------------------
2014 int FILES_STORE::DelMessage(uint64_t id, const string & login) const
2017 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2019 return unlink(fn.c_str());
2021 //-----------------------------------------------------------------------------
2022 int FILES_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
2024 vector<string> messages;
2026 dn = storeSettings.GetUsersDir() + "/" + login + "/messages/";
2027 GetFilesList(&messages, dn, S_IFREG, "");
2029 //hdrsList->resize(messages.size());
2031 for (unsigned i = 0; i < messages.size(); i++)
2033 unsigned long long id = 0;
2035 if (str2x(messages[i].c_str(), id))
2037 if (unlink((dn + messages[i]).c_str()))
2039 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2040 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2041 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2048 if (ReadMessage(dn + messages[i], &hdr, NULL))
2055 if (unlink((dn + messages[i]).c_str()))
2057 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2058 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2059 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2066 hdrsList->push_back(hdr);
2070 //-----------------------------------------------------------------------------
2071 int FILES_STORE::ReadMessage(const string & fileName,
2073 string * text) const
2076 msgFile = fopen(fileName.c_str(), "rt");
2079 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2080 errorStr = "File \'";
2081 errorStr += fileName;
2082 errorStr += "\' cannot be openned.";
2083 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
2089 d[1] = &hdr->lastSendTime;
2090 d[2] = &hdr->creationTime;
2091 d[3] = &hdr->showTime;
2092 d[4] = (unsigned*)(&hdr->repeat);
2093 d[5] = &hdr->repeatPeriod;
2095 memset(p, 0, sizeof(p));
2097 for (int pos = 0; pos < 6; pos++)
2099 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
2100 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2101 errorStr = "Cannot read file \'";
2102 errorStr += fileName;
2103 errorStr += "\'. Missing data.";
2104 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
2105 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2111 ep = strrchr(p, '\r');
2113 ep = strrchr(p, '\n');
2118 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2119 errorStr = "Cannot read file \'";
2120 errorStr += fileName;
2121 errorStr += "\'. Missing data.";
2122 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
2123 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2128 if (str2x(p, *(d[pos])))
2130 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2131 errorStr = "Cannot read file \'";
2132 errorStr += fileName;
2133 errorStr += "\'. Incorrect value. \'";
2136 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
2143 memset(txt, 0, sizeof(txt));
2146 text->erase(text->begin(), text->end());
2147 while (!feof(msgFile))
2150 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2160 //-----------------------------------------------------------------------------
2161 int FILES_STORE::Touch(const std::string & path) const
2163 FILE * f = fopen(path.c_str(), "wb");
2171 //-----------------------------------------------------------------------------