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"
63 int GetFileList(vector<string> * fileList, const string & directory, mode_t mode, const string & ext);
65 const int pt_mega = 1024 * 1024;
66 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
69 PLUGIN_CREATOR<FILES_STORE> fsc;
70 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
75 return fsc.GetPlugin();
77 //-----------------------------------------------------------------------------
78 FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
84 //-----------------------------------------------------------------------------
85 FILES_STORE_SETTINGS::~FILES_STORE_SETTINGS()
88 //-----------------------------------------------------------------------------
89 int FILES_STORE_SETTINGS::ParseOwner(const vector<PARAM_VALUE> & moduleParams, const string & owner, uid_t * uid)
93 vector<PARAM_VALUE>::const_iterator pvi;
94 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
95 if (pvi == moduleParams.end())
97 errorStr = "Parameter \'" + owner + "\' not found.";
98 printfd(__FILE__, "%s\n", errorStr.c_str());
101 if (User2UID(pvi->value[0].c_str(), uid) < 0)
103 errorStr = "Parameter \'" + owner + "\': Unknown user \'" + pvi->value[0] + "\'";
104 printfd(__FILE__, "%s\n", errorStr.c_str());
109 //-----------------------------------------------------------------------------
110 int FILES_STORE_SETTINGS::ParseGroup(const vector<PARAM_VALUE> & moduleParams, const string & group, gid_t * gid)
114 vector<PARAM_VALUE>::const_iterator pvi;
115 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
116 if (pvi == moduleParams.end())
118 errorStr = "Parameter \'" + group + "\' not found.";
119 printfd(__FILE__, "%s\n", errorStr.c_str());
122 if (Group2GID(pvi->value[0].c_str(), gid) < 0)
124 errorStr = "Parameter \'" + group + "\': Unknown group \'" + pvi->value[0] + "\'";
125 printfd(__FILE__, "%s\n", errorStr.c_str());
130 //-----------------------------------------------------------------------------
131 int FILES_STORE_SETTINGS::ParseYesNo(const string & value, bool * val)
133 if (0 == strcasecmp(value.c_str(), "yes"))
138 if (0 == strcasecmp(value.c_str(), "no"))
144 errorStr = "Incorrect value \'" + value + "\'.";
147 //-----------------------------------------------------------------------------
148 int FILES_STORE_SETTINGS::ParseMode(const vector<PARAM_VALUE> & moduleParams, const string & modeStr, mode_t * mode)
152 vector<PARAM_VALUE>::const_iterator pvi;
153 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
154 if (pvi == moduleParams.end())
156 errorStr = "Parameter \'" + modeStr + "\' not found.";
157 printfd(__FILE__, "%s\n", errorStr.c_str());
160 if (Str2Mode(pvi->value[0].c_str(), mode) < 0)
162 errorStr = "Parameter \'" + modeStr + "\': Incorrect mode \'" + pvi->value[0] + "\'";
163 printfd(__FILE__, "%s\n", errorStr.c_str());
168 //-----------------------------------------------------------------------------
169 int FILES_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
171 if (ParseOwner(s.moduleParams, "StatOwner", &statUID) < 0)
173 if (ParseGroup(s.moduleParams, "StatGroup", &statGID) < 0)
175 if (ParseMode(s.moduleParams, "StatMode", &statMode) < 0)
178 if (ParseOwner(s.moduleParams, "ConfOwner", &confUID) < 0)
180 if (ParseGroup(s.moduleParams, "ConfGroup", &confGID) < 0)
182 if (ParseMode(s.moduleParams, "ConfMode", &confMode) < 0)
185 if (ParseOwner(s.moduleParams, "UserLogOwner", &userLogUID) < 0)
187 if (ParseGroup(s.moduleParams, "UserLogGroup", &userLogGID) < 0)
189 if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0)
192 vector<PARAM_VALUE>::const_iterator pvi;
194 pv.param = "RemoveBak";
195 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
196 if (pvi == s.moduleParams.end())
202 if (ParseYesNo(pvi->value[0], &removeBak))
204 printfd(__FILE__, "Cannot parse parameter 'RemoveBak'\n");
209 pv.param = "ReadBak";
210 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
211 if (pvi == s.moduleParams.end())
217 if (ParseYesNo(pvi->value[0], &readBak))
219 printfd(__FILE__, "Cannot parse parameter 'ReadBak'\n");
224 pv.param = "WorkDir";
225 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
226 if (pvi == s.moduleParams.end())
228 errorStr = "Parameter \'WorkDir\' not found.";
229 printfd(__FILE__, "Parameter 'WorkDir' not found\n");
233 workDir = pvi->value[0];
234 if (workDir.size() && workDir[workDir.size() - 1] == '/')
236 workDir.resize(workDir.size() - 1);
238 usersDir = workDir + "/users/";
239 tariffsDir = workDir + "/tariffs/";
240 adminsDir = workDir + "/admins/";
244 //-----------------------------------------------------------------------------
245 const string & FILES_STORE_SETTINGS::GetStrError() const
249 //-----------------------------------------------------------------------------
250 int FILES_STORE_SETTINGS::User2UID(const char * user, uid_t * uid)
256 errorStr = string("User \'") + string(user) + string("\' not found in system.");
257 printfd(__FILE__, "%s\n", errorStr.c_str());
264 //-----------------------------------------------------------------------------
265 int FILES_STORE_SETTINGS::Group2GID(const char * gr, gid_t * gid)
271 errorStr = string("Group \'") + string(gr) + string("\' not found in system.");
272 printfd(__FILE__, "%s\n", errorStr.c_str());
279 //-----------------------------------------------------------------------------
280 int FILES_STORE_SETTINGS::Str2Mode(const char * str, mode_t * mode)
287 errorStr = string("Error parsing mode \'") + str + string("\'");
288 printfd(__FILE__, "%s\n", errorStr.c_str());
292 for (int i = 0; i < 3; i++)
293 if (str[i] > '7' || str[i] < '0')
295 errorStr = string("Error parsing mode \'") + str + string("\'");
296 printfd(__FILE__, "%s\n", errorStr.c_str());
304 *mode = ((mode_t)c) + ((mode_t)b << 3) + ((mode_t)a << 6);
308 //-----------------------------------------------------------------------------
309 string FILES_STORE_SETTINGS::GetWorkDir() const
313 //-----------------------------------------------------------------------------
314 string FILES_STORE_SETTINGS::GetUsersDir() const
318 //-----------------------------------------------------------------------------
319 string FILES_STORE_SETTINGS::GetAdminsDir() const
323 //-----------------------------------------------------------------------------
324 string FILES_STORE_SETTINGS::GetTariffsDir() const
328 //-----------------------------------------------------------------------------
329 mode_t FILES_STORE_SETTINGS::GetStatMode() const
333 //-----------------------------------------------------------------------------
334 mode_t FILES_STORE_SETTINGS::GetStatModeDir() const
336 mode_t mode = statMode;
337 if (statMode & S_IRUSR) mode |= S_IXUSR;
338 if (statMode & S_IRGRP) mode |= S_IXGRP;
339 if (statMode & S_IROTH) mode |= S_IXOTH;
342 //-----------------------------------------------------------------------------
343 uid_t FILES_STORE_SETTINGS::GetStatUID() const
347 //-----------------------------------------------------------------------------
348 gid_t FILES_STORE_SETTINGS::GetStatGID() const
352 //-----------------------------------------------------------------------------
353 mode_t FILES_STORE_SETTINGS::GetConfMode() const
357 //-----------------------------------------------------------------------------
358 mode_t FILES_STORE_SETTINGS::GetConfModeDir() const
360 mode_t mode = confMode;
361 if (confMode & S_IRUSR) mode |= S_IXUSR;
362 if (confMode & S_IRGRP) mode |= S_IXGRP;
363 if (confMode & S_IROTH) mode |= S_IXOTH;
366 //-----------------------------------------------------------------------------
367 uid_t FILES_STORE_SETTINGS::GetConfUID() const
371 //-----------------------------------------------------------------------------
372 gid_t FILES_STORE_SETTINGS::GetConfGID() const
376 //-----------------------------------------------------------------------------
377 mode_t FILES_STORE_SETTINGS::GetLogMode() const
381 //-----------------------------------------------------------------------------
382 uid_t FILES_STORE_SETTINGS::GetLogUID() const
386 //-----------------------------------------------------------------------------
387 gid_t FILES_STORE_SETTINGS::GetLogGID() const
391 //-----------------------------------------------------------------------------
392 bool FILES_STORE_SETTINGS::GetRemoveBak() const
396 //-----------------------------------------------------------------------------
397 bool FILES_STORE_SETTINGS::GetReadBak() const
401 //-----------------------------------------------------------------------------
402 //-----------------------------------------------------------------------------
403 //-----------------------------------------------------------------------------
404 FILES_STORE::FILES_STORE()
406 version = "file_store v.1.04";
408 pthread_mutexattr_t attr;
409 pthread_mutexattr_init(&attr);
410 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
411 pthread_mutex_init(&mutex, &attr);
413 //-----------------------------------------------------------------------------
414 FILES_STORE::~FILES_STORE()
418 //-----------------------------------------------------------------------------
419 void FILES_STORE::SetSettings(const MODULE_SETTINGS & s)
423 //-----------------------------------------------------------------------------
424 int FILES_STORE::ParseSettings()
426 int ret = storeSettings.ParseSettings(settings);
429 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
430 errorStr = storeSettings.GetStrError();
434 //-----------------------------------------------------------------------------
435 const string & FILES_STORE::GetStrError() const
437 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
440 //-----------------------------------------------------------------------------
441 const string & FILES_STORE::GetVersion() const
445 //-----------------------------------------------------------------------------
446 int FILES_STORE::GetUsersList(vector<string> * userList) const
448 vector<string> files;
450 if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, ""))
452 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
453 errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + string(strerror(errno));
457 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
459 userList->swap(files);
463 //-----------------------------------------------------------------------------
464 int FILES_STORE::GetAdminsList(vector<string> * adminList) const
466 vector<string> files;
468 if (GetFileList(&files, storeSettings.GetAdminsDir(), S_IFREG, ".adm"))
470 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
471 errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + string(strerror(errno));
475 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
477 adminList->swap(files);
481 //-----------------------------------------------------------------------------
482 int FILES_STORE::GetTariffsList(vector<string> * tariffList) const
484 vector<string> files;
486 if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf"))
488 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
489 errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + string(strerror(errno));
493 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
495 tariffList->swap(files);
499 //-----------------------------------------------------------------------------
500 int FILES_STORE::RemoveDir(const char * path) const
502 DIR * d = opendir(path);
506 errorStr = "failed to open dir. Message: '";
507 errorStr += strerror(errno);
509 printfd(__FILE__, "FILE_STORE::RemoveDir() - Failed to open dir '%s': '%s'\n", path, strerror(errno));
514 while ((entry = readdir(d)))
516 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
520 str += "/" + string(entry->d_name);
523 if (stat(str.c_str(), &st))
526 if ((st.st_mode & S_IFREG))
528 if (unlink(str.c_str()))
530 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
531 errorStr = "unlink failed. Message: '";
532 errorStr += strerror(errno);
534 printfd(__FILE__, "FILES_STORE::RemoveDir() - unlink failed. Message: '%s'\n", strerror(errno));
539 if (!(st.st_mode & S_IFDIR))
541 if (RemoveDir(str.c_str()))
553 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
554 errorStr = "rmdir failed. Message: '";
555 errorStr += strerror(errno);
557 printfd(__FILE__, "FILES_STORE::RemoveDir() - rmdir failed. Message: '%s'\n", strerror(errno));
563 //-----------------------------------------------------------------------------
564 int FILES_STORE::AddUser(const string & login) const
568 strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str());
570 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
572 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
573 errorStr = string("mkdir failed. Message: '") + strerror(errno) + "'";
574 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
578 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
581 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
582 errorStr = "Cannot create file \"" + fileName + "\'";
583 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
586 /*f = fopen(fileName.c_str(), "wt");
589 if (fprintf(f, "\n") < 0)
591 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
592 errorStr = "fprintf failed. Message: '";
593 errorStr += strerror(errno);
595 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
602 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
603 errorStr = "Cannot create file \"" + fileName + "\'";
604 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
608 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
611 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
612 errorStr = "Cannot create file \"" + fileName + "\'";
613 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
616 /*f = fopen(fileName.c_str(), "wt");
619 if (fprintf(f, "\n") < 0)
621 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
622 errorStr = "fprintf failed. Message: '";
623 errorStr += strerror(errno);
625 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
632 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
633 errorStr = "Cannot create file \"" + fileName + "\'";
634 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
639 //-----------------------------------------------------------------------------
640 int FILES_STORE::DelUser(const string & login) const
645 strprintf(&dirName, "%s/"DELETED_USERS_DIR, storeSettings.GetWorkDir().c_str());
646 if (access(dirName.c_str(), F_OK) != 0)
648 if (mkdir(dirName.c_str(), 0700) != 0)
650 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
651 errorStr = "Directory '" + dirName + "' cannot be created.";
652 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
657 if (access(dirName.c_str(), F_OK) == 0)
659 strprintf(&dirName, "%s/"DELETED_USERS_DIR"/%s.%lu", storeSettings.GetWorkDir().c_str(), login.c_str(), time(NULL));
660 strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
661 if (rename(dirName1.c_str(), dirName.c_str()))
663 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
664 errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
665 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
671 strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
672 if (RemoveDir(dirName.c_str()))
679 //-----------------------------------------------------------------------------
680 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
683 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
684 if (RestoreUserConf(conf, login, fileName))
686 if (!storeSettings.GetReadBak())
690 return RestoreUserConf(conf, login, fileName + ".bak");
694 //-----------------------------------------------------------------------------
695 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login, const string & fileName) const
697 CONFIGFILE cf(fileName);
703 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
704 errorStr = "User \'" + login + "\' data not read.";
705 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
709 if (cf.ReadString("Password", &conf->password, "") < 0)
711 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
712 errorStr = "User \'" + login + "\' data not read. Parameter Password.";
713 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
716 if (conf->password.empty())
718 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
719 errorStr = "User \'" + login + "\' password is blank.";
720 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
724 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
726 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
727 errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
728 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
731 if (conf->tariffName.empty())
733 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
734 errorStr = "User \'" + login + "\' tariff is blank.";
735 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
740 cf.ReadString("IP", &ipStr, "?");
746 catch (const string & s)
748 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
749 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
750 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
755 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
757 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
758 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
759 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
763 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
765 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
766 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
767 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
771 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
773 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
774 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
775 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
779 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
780 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
781 cf.ReadString("TariffChange", &conf->nextTariff, "");
782 cf.ReadString("Group", &conf->group, "");
783 cf.ReadString("RealName", &conf->realName, "");
784 cf.ReadString("Address", &conf->address, "");
785 cf.ReadString("Phone", &conf->phone, "");
786 cf.ReadString("Note", &conf->note, "");
787 cf.ReadString("email", &conf->email, "");
789 char userdataName[12];
790 for (int i = 0; i < USERDATA_NUM; i++)
792 snprintf(userdataName, 12, "Userdata%d", i);
793 cf.ReadString(userdataName, &conf->userdata[i], "");
796 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
798 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
799 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
800 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
806 //-----------------------------------------------------------------------------
807 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
810 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
812 if (RestoreUserStat(stat, login, fileName))
814 if (!storeSettings.GetReadBak())
818 return RestoreUserStat(stat, login, fileName + ".bak");
822 //-----------------------------------------------------------------------------
823 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login, const string & fileName) const
825 CONFIGFILE cf(fileName);
831 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
832 errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
833 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
839 for (int i = 0; i < DIR_NUM; i++)
842 snprintf(s, 22, "D%d", i);
843 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
845 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
846 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
847 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
850 stat->down[i] = traff;
852 snprintf(s, 22, "U%d", i);
853 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
855 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
856 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
857 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
863 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
865 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
866 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
867 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
871 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
873 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
874 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
875 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
879 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
881 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
882 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
883 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
887 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
889 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
890 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
891 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
895 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
897 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
898 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
899 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
903 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
905 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
906 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
907 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
913 //-----------------------------------------------------------------------------
914 int FILES_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
917 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
919 CONFIGFILE cfstat(fileName, true);
921 int e = cfstat.Error();
925 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
926 errorStr = string("User \'") + login + "\' conf not written\n";
927 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
931 e = chmod(fileName.c_str(), storeSettings.GetConfMode());
932 e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID());
936 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
937 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
940 cfstat.WriteString("Password", conf.password);
941 cfstat.WriteInt ("Passive", conf.passive);
942 cfstat.WriteInt ("Down", conf.disabled);
943 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
944 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
945 cfstat.WriteString("Tariff", conf.tariffName);
946 cfstat.WriteString("Address", conf.address);
947 cfstat.WriteString("Phone", conf.phone);
948 cfstat.WriteString("Email", conf.email);
949 cfstat.WriteString("Note", conf.note);
950 cfstat.WriteString("RealName", conf.realName);
951 cfstat.WriteString("Group", conf.group);
952 cfstat.WriteDouble("Credit", conf.credit);
953 cfstat.WriteString("TariffChange", conf.nextTariff);
955 char userdataName[12];
956 for (int i = 0; i < USERDATA_NUM; i++)
958 snprintf(userdataName, 12, "Userdata%d", i);
959 cfstat.WriteString(userdataName, conf.userdata[i]);
961 cfstat.WriteInt("CreditExpire", conf.creditExpire);
965 cfstat.WriteString("IP", ipStr.str());
969 //-----------------------------------------------------------------------------
970 int FILES_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
974 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
977 CONFIGFILE cfstat(fileName, true);
978 int e = cfstat.Error();
982 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
983 errorStr = string("User \'") + login + "\' stat not written\n";
984 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
988 for (int i = 0; i < DIR_NUM; i++)
990 snprintf(s, 22, "D%d", i);
991 cfstat.WriteInt(s, stat.down[i]);
992 snprintf(s, 22, "U%d", i);
993 cfstat.WriteInt(s, stat.up[i]);
996 cfstat.WriteDouble("Cash", stat.cash);
997 cfstat.WriteDouble("FreeMb", stat.freeMb);
998 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
999 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1000 cfstat.WriteInt("PassiveTime", stat.passiveTime);
1001 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
1004 int e = chmod(fileName.c_str(), storeSettings.GetStatMode());
1005 e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID());
1009 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1010 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1015 //-----------------------------------------------------------------------------
1016 int FILES_STORE::WriteLogString(const string & str, const string & login) const
1019 time_t tm = time(NULL);
1021 fileName = storeSettings.GetUsersDir() + "/" + login + "/log";
1022 f = fopen(fileName.c_str(), "at");
1026 fprintf(f, "%s", LogDate(tm));
1028 fprintf(f, "%s", str.c_str());
1034 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1035 errorStr = "Cannot open \'" + fileName + "\'";
1036 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1040 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1041 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1045 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1046 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1051 //-----------------------------------------------------------------------------
1052 int FILES_STORE::WriteLog2String(const string & str, const string & login) const
1055 time_t tm = time(NULL);
1057 fileName = storeSettings.GetUsersDir() + "/" + login + "/log2";
1058 f = fopen(fileName.c_str(), "at");
1062 fprintf(f, "%s", LogDate(tm));
1064 fprintf(f, "%s", str.c_str());
1070 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1071 errorStr = "Cannot open \'" + fileName + "\'";
1072 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1076 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1077 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1081 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1082 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1087 //-----------------------------------------------------------------------------
1088 int FILES_STORE::WriteUserChgLog(const string & login,
1089 const string & admLogin,
1091 const string & paramName,
1092 const string & oldValue,
1093 const string & newValue,
1094 const string & message) const
1096 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1097 + paramName + "\' parameter changed from \'" + oldValue +
1098 "\' to \'" + newValue + "\'. " + message;
1100 return WriteLogString(userLogMsg, login);
1102 //-----------------------------------------------------------------------------
1103 int FILES_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1105 string logStr = "Connect, " + inet_ntostring(ip);
1106 if (WriteLogString(logStr, login))
1108 return WriteLog2String(logStr, login);
1110 //-----------------------------------------------------------------------------
1111 int FILES_STORE::WriteUserDisconnect(const string & login,
1112 const DIR_TRAFF & up,
1113 const DIR_TRAFF & down,
1114 const DIR_TRAFF & sessionUp,
1115 const DIR_TRAFF & sessionDown,
1118 const std::string & reason) const
1120 stringstream logStr;
1121 logStr << "Disconnect, "
1122 << " session upload: \'"
1124 << "\' session download: \'"
1126 << "\' month upload: \'"
1128 << "\' month download: \'"
1134 if (WriteLogString(logStr.str(), login))
1137 logStr << " freeMb: \'"
1144 return WriteLog2String(logStr.str(), login);
1146 //-----------------------------------------------------------------------------
1147 int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const
1151 strprintf(&stat1,"%s/%s/stat.%d.%02d",
1152 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1154 CONFIGFILE s(stat1, true);
1158 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1159 errorStr = "Cannot create file '" + stat1 + "'";
1160 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1166 strprintf(&stat2,"%s/%s/stat2.%d.%02d",
1167 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1169 CONFIGFILE s2(stat2, true);
1173 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1174 errorStr = "Cannot create file '" + stat2 + "'";
1175 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1179 for (size_t i = 0; i < DIR_NUM; i++)
1182 snprintf(dirName, 3, "U%d", i);
1183 s.WriteInt(dirName, stat.up[i]); // Classic
1184 s2.WriteInt(dirName, stat.up[i]); // New
1185 snprintf(dirName, 3, "D%d", i);
1186 s.WriteInt(dirName, stat.down[i]); // Classic
1187 s2.WriteInt(dirName, stat.down[i]); // New
1191 s.WriteDouble("cash", stat.cash);
1194 s2.WriteDouble("Cash", stat.cash);
1195 s2.WriteDouble("FreeMb", stat.freeMb);
1196 s2.WriteDouble("LastCashAdd", stat.lastCashAdd);
1197 s2.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1198 s2.WriteInt("PassiveTime", stat.passiveTime);
1199 s2.WriteInt("LastActivityTime", stat.lastActivityTime);
1203 //-----------------------------------------------------------------------------*/
1204 int FILES_STORE::AddAdmin(const string & login) const
1207 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1209 if (Touch(fileName))
1211 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1212 errorStr = "Cannot create file " + fileName;
1213 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1219 //-----------------------------------------------------------------------------*/
1220 int FILES_STORE::DelAdmin(const string & login) const
1223 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1224 if (unlink(fileName.c_str()))
1226 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1227 errorStr = "unlink failed. Message: '";
1228 errorStr += strerror(errno);
1230 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1234 //-----------------------------------------------------------------------------*/
1235 int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1237 char passwordE[2 * ADM_PASSWD_LEN + 2];
1238 char pass[ADM_PASSWD_LEN + 1];
1239 char adminPass[ADM_PASSWD_LEN + 1];
1243 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1246 CONFIGFILE cf(fileName, true);
1252 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1253 errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1254 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1258 memset(pass, 0, sizeof(pass));
1259 memset(adminPass, 0, sizeof(adminPass));
1262 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1264 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1265 adminPass[ADM_PASSWD_LEN - 1] = 0;
1267 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1269 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1272 pass[ADM_PASSWD_LEN - 1] = 0;
1273 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1275 cf.WriteString("password", passwordE);
1276 cf.WriteInt("ChgConf", ac.priv.userConf);
1277 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1278 cf.WriteInt("ChgStat", ac.priv.userStat);
1279 cf.WriteInt("ChgCash", ac.priv.userCash);
1280 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1281 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1282 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1287 //-----------------------------------------------------------------------------
1288 int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1291 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1292 CONFIGFILE cf(fileName);
1293 char pass[ADM_PASSWD_LEN + 1];
1294 char password[ADM_PASSWD_LEN + 1];
1295 char passwordE[2 * ADM_PASSWD_LEN + 2];
1302 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1303 errorStr = "Cannot open " + fileName;
1304 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1310 if (cf.ReadString("password", &p, "*"))
1312 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1313 errorStr = "Error in parameter password";
1314 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1318 memset(passwordE, 0, sizeof(passwordE));
1319 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1321 memset(pass, 0, sizeof(pass));
1323 if (passwordE[0] != 0)
1325 Decode21(pass, passwordE);
1326 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1328 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1330 DecodeString(password + 8*i, pass + 8*i, &ctx);
1338 ac->password = password;
1340 if (cf.ReadInt("ChgConf", &a, 0) == 0)
1341 ac->priv.userConf = a;
1344 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1345 errorStr = "Error in parameter ChgConf";
1346 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1350 if (cf.ReadInt("ChgPassword", &a, 0) == 0)
1351 ac->priv.userPasswd = a;
1354 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1355 errorStr = "Error in parameter ChgPassword";
1356 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1360 if (cf.ReadInt("ChgStat", &a, 0) == 0)
1361 ac->priv.userStat = a;
1364 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1365 errorStr = "Error in parameter ChgStat";
1366 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1370 if (cf.ReadInt("ChgCash", &a, 0) == 0)
1371 ac->priv.userCash = a;
1374 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1375 errorStr = "Error in parameter ChgCash";
1376 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1380 if (cf.ReadInt("UsrAddDel", &a, 0) == 0)
1381 ac->priv.userAddDel = a;
1384 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1385 errorStr = "Error in parameter UsrAddDel";
1386 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1390 if (cf.ReadInt("ChgAdmin", &a, 0) == 0)
1391 ac->priv.adminChg = a;
1394 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1395 errorStr = "Error in parameter ChgAdmin";
1396 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1400 if (cf.ReadInt("ChgTariff", &a, 0) == 0)
1401 ac->priv.tariffChg = a;
1404 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1405 errorStr = "Error in parameter ChgTariff";
1406 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1412 //-----------------------------------------------------------------------------
1413 int FILES_STORE::AddTariff(const string & name) const
1416 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1417 if (Touch(fileName))
1419 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1420 errorStr = "Cannot create file " + fileName;
1421 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1426 //-----------------------------------------------------------------------------
1427 int FILES_STORE::DelTariff(const string & name) const
1430 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1431 if (unlink(fileName.c_str()))
1433 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1434 errorStr = "unlink failed. Message: '";
1435 errorStr += strerror(errno);
1437 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1441 //-----------------------------------------------------------------------------
1442 int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1444 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1445 CONFIGFILE conf(fileName);
1447 td->tariffConf.name = tariffName;
1449 if (conf.Error() != 0)
1451 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1452 errorStr = "Cannot read file " + fileName;
1453 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1458 for (int i = 0; i<DIR_NUM; i++)
1460 strprintf(¶m, "Time%d", i);
1461 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1463 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1464 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1465 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1469 ParseTariffTimeStr(str.c_str(),
1470 td->dirPrice[i].hDay,
1471 td->dirPrice[i].mDay,
1472 td->dirPrice[i].hNight,
1473 td->dirPrice[i].mNight);
1475 strprintf(¶m, "PriceDayA%d", i);
1476 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1478 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1479 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1480 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1483 td->dirPrice[i].priceDayA /= (1024*1024);
1485 strprintf(¶m, "PriceDayB%d", i);
1486 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1488 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1489 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1490 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1493 td->dirPrice[i].priceDayB /= (1024*1024);
1495 strprintf(¶m, "PriceNightA%d", i);
1496 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1498 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1499 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1500 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1503 td->dirPrice[i].priceNightA /= (1024*1024);
1505 strprintf(¶m, "PriceNightB%d", i);
1506 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1508 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1509 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1510 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1513 td->dirPrice[i].priceNightB /= (1024*1024);
1515 strprintf(¶m, "Threshold%d", i);
1516 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1518 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1519 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1520 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1524 strprintf(¶m, "SinglePrice%d", i);
1525 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1527 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1528 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1529 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1533 strprintf(¶m, "NoDiscount%d", i);
1534 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1536 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1537 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1538 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1543 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1545 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1546 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1547 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1551 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1553 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1554 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1555 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1559 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1561 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1562 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1563 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1567 if (conf.ReadString("TraffType", &str, "") < 0)
1569 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1570 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1571 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1575 if (!strcasecmp(str.c_str(), "up"))
1576 td->tariffConf.traffType = TRAFF_UP;
1578 if (!strcasecmp(str.c_str(), "down"))
1579 td->tariffConf.traffType = TRAFF_DOWN;
1581 if (!strcasecmp(str.c_str(), "up+down"))
1582 td->tariffConf.traffType = TRAFF_UP_DOWN;
1584 if (!strcasecmp(str.c_str(), "max"))
1585 td->tariffConf.traffType = TRAFF_MAX;
1588 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1589 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1590 printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str());
1595 //-----------------------------------------------------------------------------
1596 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1598 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1601 CONFIGFILE cf(fileName, true);
1607 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1608 errorStr = "Error writing tariff " + tariffName;
1609 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1614 for (int i = 0; i < DIR_NUM; i++)
1616 strprintf(¶m, "PriceDayA%d", i);
1617 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1619 strprintf(¶m, "PriceDayB%d", i);
1620 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1622 strprintf(¶m, "PriceNightA%d", i);
1623 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1625 strprintf(¶m, "PriceNightB%d", i);
1626 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1628 strprintf(¶m, "Threshold%d", i);
1629 cf.WriteInt(param, td.dirPrice[i].threshold);
1632 strprintf(¶m, "Time%d", i);
1634 strprintf(&s, "%0d:%0d-%0d:%0d",
1635 td.dirPrice[i].hDay,
1636 td.dirPrice[i].mDay,
1637 td.dirPrice[i].hNight,
1638 td.dirPrice[i].mNight);
1640 cf.WriteString(param, s);
1642 strprintf(¶m, "NoDiscount%d", i);
1643 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1645 strprintf(¶m, "SinglePrice%d", i);
1646 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1649 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1650 cf.WriteDouble("Fee", td.tariffConf.fee);
1651 cf.WriteDouble("Free", td.tariffConf.free);
1653 switch (td.tariffConf.traffType)
1656 cf.WriteString("TraffType", "up");
1659 cf.WriteString("TraffType", "down");
1662 cf.WriteString("TraffType", "up+down");
1665 cf.WriteString("TraffType", "max");
1672 //-----------------------------------------------------------------------------
1673 int FILES_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1675 const string & login) const
1677 char fn[FN_STR_LEN];
1678 char dn[FN_STR_LEN];
1685 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1686 if (access(dn, F_OK) != 0)
1688 if (mkdir(dn, 0700) != 0)
1690 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1691 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1692 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1697 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1698 e += chmod(dn, storeSettings.GetStatModeDir());
1702 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1703 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1708 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1714 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1715 storeSettings.GetUsersDir().c_str(),
1719 if (access(dn, F_OK) != 0)
1721 if (mkdir(dn, 0700) != 0)
1723 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1724 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1725 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1730 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1731 e += chmod(dn, storeSettings.GetStatModeDir());
1735 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1736 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1739 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1740 storeSettings.GetUsersDir().c_str(),
1743 lt->tm_mon+1 < 10 ? "0" : "",
1745 if (access(dn, F_OK) != 0)
1747 if (mkdir(dn, 0700) != 0)
1749 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1750 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1751 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1756 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1757 e += chmod(dn, storeSettings.GetStatModeDir());
1761 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1762 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1765 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1767 statFile = fopen (fn, "at");
1771 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1772 errorStr = "File \'" + string(fn) + "\' cannot be written.";
1773 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1780 lt1 = localtime(&lastStat);
1789 lt2 = localtime(&t);
1795 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1796 h1, m1, s1, h2, m2, s2) < 0)
1798 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1799 errorStr = string("fprint failed. Message: '") + strerror(errno) + "'";
1800 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1805 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1806 stIter = statTree.begin();
1808 while (stIter != statTree.end())
1811 x2str(stIter->second.up, u);
1812 x2str(stIter->second.down, d);
1813 #ifdef TRAFF_STAT_WITH_PORTS
1814 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1815 inet_ntostring(stIter->first.ip).c_str(),
1820 stIter->second.cash) < 0)
1822 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1823 errorStr = "fprint failed. Message: '";
1824 errorStr += strerror(errno);
1826 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1831 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1832 inet_ntostring(stIter->first.ip).c_str(),
1836 stIter->second.cash) < 0)
1838 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1839 errorStr = string("fprint failed. Message: '");
1840 errorStr += strerror(errno);
1842 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1853 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1854 e += chmod(fn, storeSettings.GetStatMode());
1858 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1859 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1864 //-----------------------------------------------------------------------------
1865 int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const
1867 //ðÒÏ×ÅÒÉÔØ ÅÓÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1868 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1869 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1875 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1876 if (access(dn.c_str(), F_OK) != 0)
1878 if (mkdir(dn.c_str(), 0700) != 0)
1880 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1881 errorStr = "Directory \'";
1883 errorStr += "\' cannot be created.";
1884 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1889 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1891 gettimeofday(&tv, NULL);
1893 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1894 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1898 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1899 errorStr = "File \'";
1901 errorStr += "\' cannot be writen.";
1902 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1906 return EditMessage(*msg, login);
1908 //-----------------------------------------------------------------------------
1909 int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1911 //ðÒÏ×ÅÒÉÔØ ÅÓÌÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1912 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1913 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1918 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1920 if (access(fileName.c_str(), F_OK) != 0)
1923 x2str(msg.header.id, idstr);
1924 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1925 errorStr = "Message for user \'";
1926 errorStr += login + "\' with ID \'";
1927 errorStr += idstr + "\' does not exist.";
1928 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
1932 Touch(fileName + ".new");
1934 msgFile = fopen((fileName + ".new").c_str(), "wt");
1937 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1938 errorStr = "File \'" + fileName + "\' cannot be writen.";
1939 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1944 res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0);
1945 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1946 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1947 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1948 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1949 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1950 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1954 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1955 errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'";
1956 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
1963 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
1965 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1967 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1968 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1969 printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno));
1975 //-----------------------------------------------------------------------------
1976 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1979 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1980 msg->header.id = id;
1981 return ReadMessage(fn, &msg->header, &msg->text);
1983 //-----------------------------------------------------------------------------
1984 int FILES_STORE::DelMessage(uint64_t id, const string & login) const
1987 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1989 return unlink(fn.c_str());
1991 //-----------------------------------------------------------------------------
1992 int FILES_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
1994 string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/");
1996 //hdrsList->resize(messages.size());
1998 if (access(dn.c_str(), F_OK) != 0)
2003 vector<string> messages;
2004 GetFileList(&messages, dn, S_IFREG, "");
2006 for (unsigned i = 0; i < messages.size(); i++)
2008 unsigned long long id = 0;
2010 if (str2x(messages[i].c_str(), id))
2012 if (unlink((dn + messages[i]).c_str()))
2014 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2015 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2016 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2023 if (ReadMessage(dn + messages[i], &hdr, NULL))
2030 if (unlink((dn + messages[i]).c_str()))
2032 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2033 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2034 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2041 hdrsList->push_back(hdr);
2045 //-----------------------------------------------------------------------------
2046 int FILES_STORE::ReadMessage(const string & fileName,
2048 string * text) const
2051 msgFile = fopen(fileName.c_str(), "rt");
2054 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2055 errorStr = "File \'";
2056 errorStr += fileName;
2057 errorStr += "\' cannot be openned.";
2058 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
2064 d[1] = &hdr->lastSendTime;
2065 d[2] = &hdr->creationTime;
2066 d[3] = &hdr->showTime;
2067 d[4] = (unsigned*)(&hdr->repeat);
2068 d[5] = &hdr->repeatPeriod;
2070 memset(p, 0, sizeof(p));
2072 for (int pos = 0; pos < 6; pos++)
2074 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
2075 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2076 errorStr = "Cannot read file \'";
2077 errorStr += fileName;
2078 errorStr += "\'. Missing data.";
2079 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
2080 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2086 ep = strrchr(p, '\r');
2088 ep = strrchr(p, '\n');
2093 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2094 errorStr = "Cannot read file \'";
2095 errorStr += fileName;
2096 errorStr += "\'. Missing data.";
2097 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
2098 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2103 if (str2x(p, *(d[pos])))
2105 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2106 errorStr = "Cannot read file \'";
2107 errorStr += fileName;
2108 errorStr += "\'. Incorrect value. \'";
2111 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
2118 memset(txt, 0, sizeof(txt));
2121 text->erase(text->begin(), text->end());
2122 while (!feof(msgFile))
2125 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2135 //-----------------------------------------------------------------------------
2136 int FILES_STORE::Touch(const std::string & path) const
2138 FILE * f = fopen(path.c_str(), "wb");
2146 //-----------------------------------------------------------------------------
2147 int GetFileList(vector<string> * fileList, const string & directory, mode_t mode, const string & ext)
2149 // æÕÎËÃÉÑ ÐÒÏÓÍÁÔÒÉ×ÁÅÔ ÓÏÄÅÒÖÉÍÏÅ ÄÉÒÅËÔÏÒÉÉ
2151 DIR * d = opendir(directory.c_str());
2155 printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno));
2160 while ((entry = readdir(d)))
2162 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
2165 string str = directory + "/" + string(entry->d_name);
2168 if (stat(str.c_str(), &st))
2171 if (!(st.st_mode & mode)) // Filter by mode
2177 size_t d_nameLen = strlen(entry->d_name);
2178 if (d_nameLen <= ext.size())
2181 if (ext == entry->d_name + (d_nameLen - ext.size()))
2183 entry->d_name[d_nameLen - ext.size()] = 0;
2184 fileList->push_back(entry->d_name);
2189 fileList->push_back(entry->d_name);
2197 //-----------------------------------------------------------------------------