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);
1283 cf.WriteInt("ChgService", ac.priv.serviceChg);
1284 cf.WriteInt("ChgCorp", ac.priv.corpChg);
1289 //-----------------------------------------------------------------------------
1290 int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1293 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1294 CONFIGFILE cf(fileName);
1295 char pass[ADM_PASSWD_LEN + 1];
1296 char password[ADM_PASSWD_LEN + 1];
1297 char passwordE[2 * ADM_PASSWD_LEN + 2];
1304 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1305 errorStr = "Cannot open " + fileName;
1306 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1312 if (cf.ReadString("password", &p, "*"))
1314 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1315 errorStr = "Error in parameter password";
1316 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1320 memset(passwordE, 0, sizeof(passwordE));
1321 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1323 memset(pass, 0, sizeof(pass));
1325 if (passwordE[0] != 0)
1327 Decode21(pass, passwordE);
1328 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1330 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1332 DecodeString(password + 8*i, pass + 8*i, &ctx);
1340 ac->password = password;
1342 if (cf.ReadInt("ChgConf", &a, 0) == 0)
1343 ac->priv.userConf = a;
1346 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1347 errorStr = "Error in parameter ChgConf";
1348 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1352 if (cf.ReadInt("ChgPassword", &a, 0) == 0)
1353 ac->priv.userPasswd = a;
1356 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1357 errorStr = "Error in parameter ChgPassword";
1358 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1362 if (cf.ReadInt("ChgStat", &a, 0) == 0)
1363 ac->priv.userStat = a;
1366 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1367 errorStr = "Error in parameter ChgStat";
1368 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1372 if (cf.ReadInt("ChgCash", &a, 0) == 0)
1373 ac->priv.userCash = a;
1376 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1377 errorStr = "Error in parameter ChgCash";
1378 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1382 if (cf.ReadInt("UsrAddDel", &a, 0) == 0)
1383 ac->priv.userAddDel = a;
1386 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1387 errorStr = "Error in parameter UsrAddDel";
1388 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1392 if (cf.ReadInt("ChgAdmin", &a, 0) == 0)
1393 ac->priv.adminChg = a;
1396 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1397 errorStr = "Error in parameter ChgAdmin";
1398 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1402 if (cf.ReadInt("ChgTariff", &a, 0) == 0)
1403 ac->priv.tariffChg = a;
1406 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1407 errorStr = "Error in parameter ChgTariff";
1408 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1412 if (cf.ReadInt("ChgService", &a, 0) == 0)
1413 ac->priv.serviceChg = a;
1415 ac->priv.serviceChg = 0;
1417 if (cf.ReadInt("ChgCorp", &a, 0) == 0)
1418 ac->priv.corpChg = a;
1420 ac->priv.corpChg = 0;
1424 //-----------------------------------------------------------------------------
1425 int FILES_STORE::AddTariff(const string & name) const
1428 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1429 if (Touch(fileName))
1431 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1432 errorStr = "Cannot create file " + fileName;
1433 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1438 //-----------------------------------------------------------------------------
1439 int FILES_STORE::DelTariff(const string & name) const
1442 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1443 if (unlink(fileName.c_str()))
1445 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1446 errorStr = "unlink failed. Message: '";
1447 errorStr += strerror(errno);
1449 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1453 //-----------------------------------------------------------------------------
1454 int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1456 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1457 CONFIGFILE conf(fileName);
1459 td->tariffConf.name = tariffName;
1461 if (conf.Error() != 0)
1463 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1464 errorStr = "Cannot read file " + fileName;
1465 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1470 for (int i = 0; i<DIR_NUM; i++)
1472 strprintf(¶m, "Time%d", i);
1473 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1475 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1476 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1477 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1481 ParseTariffTimeStr(str.c_str(),
1482 td->dirPrice[i].hDay,
1483 td->dirPrice[i].mDay,
1484 td->dirPrice[i].hNight,
1485 td->dirPrice[i].mNight);
1487 strprintf(¶m, "PriceDayA%d", i);
1488 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1490 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1491 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1492 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1495 td->dirPrice[i].priceDayA /= (1024*1024);
1497 strprintf(¶m, "PriceDayB%d", i);
1498 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1500 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1501 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1502 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1505 td->dirPrice[i].priceDayB /= (1024*1024);
1507 strprintf(¶m, "PriceNightA%d", i);
1508 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1510 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1511 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1512 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1515 td->dirPrice[i].priceNightA /= (1024*1024);
1517 strprintf(¶m, "PriceNightB%d", i);
1518 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1520 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1521 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1522 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1525 td->dirPrice[i].priceNightB /= (1024*1024);
1527 strprintf(¶m, "Threshold%d", i);
1528 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1530 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1531 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1532 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1536 strprintf(¶m, "SinglePrice%d", i);
1537 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1539 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1540 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1541 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1545 strprintf(¶m, "NoDiscount%d", i);
1546 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1548 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1549 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1550 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1555 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1557 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1558 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1559 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1563 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1565 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1566 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1567 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1571 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1573 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1574 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1575 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1579 if (conf.ReadString("TraffType", &str, "") < 0)
1581 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1582 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1583 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1587 if (!strcasecmp(str.c_str(), "up"))
1588 td->tariffConf.traffType = TRAFF_UP;
1590 if (!strcasecmp(str.c_str(), "down"))
1591 td->tariffConf.traffType = TRAFF_DOWN;
1593 if (!strcasecmp(str.c_str(), "up+down"))
1594 td->tariffConf.traffType = TRAFF_UP_DOWN;
1596 if (!strcasecmp(str.c_str(), "max"))
1597 td->tariffConf.traffType = TRAFF_MAX;
1600 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1601 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1602 printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str());
1607 //-----------------------------------------------------------------------------
1608 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1610 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1613 CONFIGFILE cf(fileName, true);
1619 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1620 errorStr = "Error writing tariff " + tariffName;
1621 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1626 for (int i = 0; i < DIR_NUM; i++)
1628 strprintf(¶m, "PriceDayA%d", i);
1629 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1631 strprintf(¶m, "PriceDayB%d", i);
1632 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1634 strprintf(¶m, "PriceNightA%d", i);
1635 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1637 strprintf(¶m, "PriceNightB%d", i);
1638 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1640 strprintf(¶m, "Threshold%d", i);
1641 cf.WriteInt(param, td.dirPrice[i].threshold);
1644 strprintf(¶m, "Time%d", i);
1646 strprintf(&s, "%0d:%0d-%0d:%0d",
1647 td.dirPrice[i].hDay,
1648 td.dirPrice[i].mDay,
1649 td.dirPrice[i].hNight,
1650 td.dirPrice[i].mNight);
1652 cf.WriteString(param, s);
1654 strprintf(¶m, "NoDiscount%d", i);
1655 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1657 strprintf(¶m, "SinglePrice%d", i);
1658 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1661 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1662 cf.WriteDouble("Fee", td.tariffConf.fee);
1663 cf.WriteDouble("Free", td.tariffConf.free);
1665 switch (td.tariffConf.traffType)
1668 cf.WriteString("TraffType", "up");
1671 cf.WriteString("TraffType", "down");
1674 cf.WriteString("TraffType", "up+down");
1677 cf.WriteString("TraffType", "max");
1684 //-----------------------------------------------------------------------------
1685 int FILES_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1687 const string & login) const
1689 char fn[FN_STR_LEN];
1690 char dn[FN_STR_LEN];
1697 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1698 if (access(dn, F_OK) != 0)
1700 if (mkdir(dn, 0700) != 0)
1702 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1703 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1704 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1709 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1710 e += chmod(dn, storeSettings.GetStatModeDir());
1714 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1715 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1720 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1726 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1727 storeSettings.GetUsersDir().c_str(),
1731 if (access(dn, F_OK) != 0)
1733 if (mkdir(dn, 0700) != 0)
1735 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1736 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1737 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1742 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1743 e += chmod(dn, storeSettings.GetStatModeDir());
1747 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1748 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1751 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1752 storeSettings.GetUsersDir().c_str(),
1755 lt->tm_mon+1 < 10 ? "0" : "",
1757 if (access(dn, F_OK) != 0)
1759 if (mkdir(dn, 0700) != 0)
1761 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1762 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1763 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1768 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1769 e += chmod(dn, storeSettings.GetStatModeDir());
1773 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1774 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1777 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1779 statFile = fopen (fn, "at");
1783 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1784 errorStr = "File \'" + string(fn) + "\' cannot be written.";
1785 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1792 lt1 = localtime(&lastStat);
1801 lt2 = localtime(&t);
1807 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1808 h1, m1, s1, h2, m2, s2) < 0)
1810 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1811 errorStr = string("fprint failed. Message: '") + strerror(errno) + "'";
1812 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1817 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1818 stIter = statTree.begin();
1820 while (stIter != statTree.end())
1823 x2str(stIter->second.up, u);
1824 x2str(stIter->second.down, d);
1825 #ifdef TRAFF_STAT_WITH_PORTS
1826 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1827 inet_ntostring(stIter->first.ip).c_str(),
1832 stIter->second.cash) < 0)
1834 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1835 errorStr = "fprint failed. Message: '";
1836 errorStr += strerror(errno);
1838 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1843 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1844 inet_ntostring(stIter->first.ip).c_str(),
1848 stIter->second.cash) < 0)
1850 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1851 errorStr = string("fprint failed. Message: '");
1852 errorStr += strerror(errno);
1854 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1865 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1866 e += chmod(fn, storeSettings.GetStatMode());
1870 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1871 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1876 //-----------------------------------------------------------------------------
1877 int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const
1879 //ðÒÏ×ÅÒÉÔØ ÅÓÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1880 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1881 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1887 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1888 if (access(dn.c_str(), F_OK) != 0)
1890 if (mkdir(dn.c_str(), 0700) != 0)
1892 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1893 errorStr = "Directory \'";
1895 errorStr += "\' cannot be created.";
1896 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1901 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1903 gettimeofday(&tv, NULL);
1905 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1906 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1910 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1911 errorStr = "File \'";
1913 errorStr += "\' cannot be writen.";
1914 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1918 return EditMessage(*msg, login);
1920 //-----------------------------------------------------------------------------
1921 int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1923 //ðÒÏ×ÅÒÉÔØ ÅÓÌÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1924 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1925 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1930 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1932 if (access(fileName.c_str(), F_OK) != 0)
1935 x2str(msg.header.id, idstr);
1936 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1937 errorStr = "Message for user \'";
1938 errorStr += login + "\' with ID \'";
1939 errorStr += idstr + "\' does not exist.";
1940 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
1944 Touch(fileName + ".new");
1946 msgFile = fopen((fileName + ".new").c_str(), "wt");
1949 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1950 errorStr = "File \'" + fileName + "\' cannot be writen.";
1951 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1956 res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0);
1957 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1958 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1959 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1960 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1961 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1962 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1966 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1967 errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'";
1968 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
1975 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
1977 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1979 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1980 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1981 printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno));
1987 //-----------------------------------------------------------------------------
1988 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1991 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1992 msg->header.id = id;
1993 return ReadMessage(fn, &msg->header, &msg->text);
1995 //-----------------------------------------------------------------------------
1996 int FILES_STORE::DelMessage(uint64_t id, const string & login) const
1999 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2001 return unlink(fn.c_str());
2003 //-----------------------------------------------------------------------------
2004 int FILES_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
2006 string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/");
2008 //hdrsList->resize(messages.size());
2010 if (access(dn.c_str(), F_OK) != 0)
2015 vector<string> messages;
2016 GetFileList(&messages, dn, S_IFREG, "");
2018 for (unsigned i = 0; i < messages.size(); i++)
2020 unsigned long long id = 0;
2022 if (str2x(messages[i].c_str(), id))
2024 if (unlink((dn + messages[i]).c_str()))
2026 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2027 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2028 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2035 if (ReadMessage(dn + messages[i], &hdr, NULL))
2042 if (unlink((dn + messages[i]).c_str()))
2044 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2045 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2046 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2053 hdrsList->push_back(hdr);
2057 //-----------------------------------------------------------------------------
2058 int FILES_STORE::ReadMessage(const string & fileName,
2060 string * text) const
2063 msgFile = fopen(fileName.c_str(), "rt");
2066 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2067 errorStr = "File \'";
2068 errorStr += fileName;
2069 errorStr += "\' cannot be openned.";
2070 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
2076 d[1] = &hdr->lastSendTime;
2077 d[2] = &hdr->creationTime;
2078 d[3] = &hdr->showTime;
2079 d[4] = (unsigned*)(&hdr->repeat);
2080 d[5] = &hdr->repeatPeriod;
2082 memset(p, 0, sizeof(p));
2084 for (int pos = 0; pos < 6; pos++)
2086 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
2087 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2088 errorStr = "Cannot read file \'";
2089 errorStr += fileName;
2090 errorStr += "\'. Missing data.";
2091 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
2092 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2098 ep = strrchr(p, '\r');
2100 ep = strrchr(p, '\n');
2105 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2106 errorStr = "Cannot read file \'";
2107 errorStr += fileName;
2108 errorStr += "\'. Missing data.";
2109 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
2110 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2115 if (str2x(p, *(d[pos])))
2117 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2118 errorStr = "Cannot read file \'";
2119 errorStr += fileName;
2120 errorStr += "\'. Incorrect value. \'";
2123 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
2130 memset(txt, 0, sizeof(txt));
2133 text->erase(text->begin(), text->end());
2134 while (!feof(msgFile))
2137 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2147 //-----------------------------------------------------------------------------
2148 int FILES_STORE::Touch(const std::string & path) const
2150 FILE * f = fopen(path.c_str(), "wb");
2158 //-----------------------------------------------------------------------------
2159 int GetFileList(vector<string> * fileList, const string & directory, mode_t mode, const string & ext)
2161 // æÕÎËÃÉÑ ÐÒÏÓÍÁÔÒÉ×ÁÅÔ ÓÏÄÅÒÖÉÍÏÅ ÄÉÒÅËÔÏÒÉÉ
2163 DIR * d = opendir(directory.c_str());
2167 printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno));
2172 while ((entry = readdir(d)))
2174 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
2177 string str = directory + "/" + string(entry->d_name);
2180 if (stat(str.c_str(), &st))
2183 if (!(st.st_mode & mode)) // Filter by mode
2189 size_t d_nameLen = strlen(entry->d_name);
2190 if (d_nameLen <= ext.size())
2193 if (ext == entry->d_name + (d_nameLen - ext.size()))
2195 entry->d_name[d_nameLen - ext.size()] = 0;
2196 fileList->push_back(entry->d_name);
2201 fileList->push_back(entry->d_name);
2209 //-----------------------------------------------------------------------------