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));
540 if (!(st.st_mode & S_IFDIR))
542 if (RemoveDir(str.c_str()))
555 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
556 errorStr = "rmdir failed. Message: '";
557 errorStr += strerror(errno);
559 printfd(__FILE__, "FILES_STORE::RemoveDir() - rmdir failed. Message: '%s'\n", strerror(errno));
565 //-----------------------------------------------------------------------------
566 int FILES_STORE::AddUser(const string & login) const
570 strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str());
572 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
574 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
575 errorStr = string("mkdir failed. Message: '") + strerror(errno) + "'";
576 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
580 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
583 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
584 errorStr = "Cannot create file \"" + fileName + "\'";
585 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
588 /*f = fopen(fileName.c_str(), "wt");
591 if (fprintf(f, "\n") < 0)
593 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
594 errorStr = "fprintf failed. Message: '";
595 errorStr += strerror(errno);
597 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
604 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
605 errorStr = "Cannot create file \"" + fileName + "\'";
606 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
610 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
613 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
614 errorStr = "Cannot create file \"" + fileName + "\'";
615 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
618 /*f = fopen(fileName.c_str(), "wt");
621 if (fprintf(f, "\n") < 0)
623 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
624 errorStr = "fprintf failed. Message: '";
625 errorStr += strerror(errno);
627 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
634 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
635 errorStr = "Cannot create file \"" + fileName + "\'";
636 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
641 //-----------------------------------------------------------------------------
642 int FILES_STORE::DelUser(const string & login) const
647 strprintf(&dirName, "%s/"DELETED_USERS_DIR, storeSettings.GetWorkDir().c_str());
648 if (access(dirName.c_str(), F_OK) != 0)
650 if (mkdir(dirName.c_str(), 0700) != 0)
652 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
653 errorStr = "Directory '" + dirName + "' cannot be created.";
654 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
659 if (access(dirName.c_str(), F_OK) == 0)
661 strprintf(&dirName, "%s/"DELETED_USERS_DIR"/%s.%lu", storeSettings.GetWorkDir().c_str(), login.c_str(), time(NULL));
662 strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
663 if (rename(dirName1.c_str(), dirName.c_str()))
665 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
666 errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
667 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
673 strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
674 if (RemoveDir(dirName.c_str()))
681 //-----------------------------------------------------------------------------
682 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
685 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
686 if (RestoreUserConf(conf, login, fileName))
688 if (!storeSettings.GetReadBak())
692 return RestoreUserConf(conf, login, fileName + ".bak");
696 //-----------------------------------------------------------------------------
697 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login, const string & fileName) const
699 CONFIGFILE cf(fileName);
705 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
706 errorStr = "User \'" + login + "\' data not read.";
707 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
711 if (cf.ReadString("Password", &conf->password, "") < 0)
713 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
714 errorStr = "User \'" + login + "\' data not read. Parameter Password.";
715 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
718 if (conf->password.empty())
720 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
721 errorStr = "User \'" + login + "\' password is blank.";
722 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
726 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
728 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
729 errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
730 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
733 if (conf->tariffName.empty())
735 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
736 errorStr = "User \'" + login + "\' tariff is blank.";
737 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
742 cf.ReadString("IP", &ipStr, "?");
748 catch (const string & s)
750 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
751 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
752 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
757 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
759 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
760 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
761 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
765 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
767 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
768 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
769 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
773 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
775 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
776 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
777 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
781 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
782 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
783 cf.ReadString("TariffChange", &conf->nextTariff, "");
784 cf.ReadString("Group", &conf->group, "");
785 cf.ReadString("RealName", &conf->realName, "");
786 cf.ReadString("Address", &conf->address, "");
787 cf.ReadString("Phone", &conf->phone, "");
788 cf.ReadString("Note", &conf->note, "");
789 cf.ReadString("email", &conf->email, "");
791 char userdataName[12];
792 for (int i = 0; i < USERDATA_NUM; i++)
794 snprintf(userdataName, 12, "Userdata%d", i);
795 cf.ReadString(userdataName, &conf->userdata[i], "");
798 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
800 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
801 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
802 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
808 //-----------------------------------------------------------------------------
809 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
812 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
814 if (RestoreUserStat(stat, login, fileName))
816 if (!storeSettings.GetReadBak())
820 return RestoreUserStat(stat, login, fileName + ".bak");
824 //-----------------------------------------------------------------------------
825 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login, const string & fileName) const
827 CONFIGFILE cf(fileName);
833 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
834 errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
835 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
841 for (int i = 0; i < DIR_NUM; i++)
844 snprintf(s, 22, "D%d", i);
845 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
847 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
848 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
849 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
852 stat->down[i] = traff;
854 snprintf(s, 22, "U%d", i);
855 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
857 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
858 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
859 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
865 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
867 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
868 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
869 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
873 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
875 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
876 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
877 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
881 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
883 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
884 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
885 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
889 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
891 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
892 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
893 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
897 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
899 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
900 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
901 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
905 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
907 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
908 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
909 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
915 //-----------------------------------------------------------------------------
916 int FILES_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
919 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
921 CONFIGFILE cfstat(fileName, true);
923 int e = cfstat.Error();
927 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
928 errorStr = string("User \'") + login + "\' conf not written\n";
929 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
933 e = chmod(fileName.c_str(), storeSettings.GetConfMode());
934 e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID());
938 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
939 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
942 cfstat.WriteString("Password", conf.password);
943 cfstat.WriteInt ("Passive", conf.passive);
944 cfstat.WriteInt ("Down", conf.disabled);
945 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
946 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
947 cfstat.WriteString("Tariff", conf.tariffName);
948 cfstat.WriteString("Address", conf.address);
949 cfstat.WriteString("Phone", conf.phone);
950 cfstat.WriteString("Email", conf.email);
951 cfstat.WriteString("Note", conf.note);
952 cfstat.WriteString("RealName", conf.realName);
953 cfstat.WriteString("Group", conf.group);
954 cfstat.WriteDouble("Credit", conf.credit);
955 cfstat.WriteString("TariffChange", conf.nextTariff);
957 char userdataName[12];
958 for (int i = 0; i < USERDATA_NUM; i++)
960 snprintf(userdataName, 12, "Userdata%d", i);
961 cfstat.WriteString(userdataName, conf.userdata[i]);
963 cfstat.WriteInt("CreditExpire", conf.creditExpire);
967 cfstat.WriteString("IP", ipStr.str());
971 //-----------------------------------------------------------------------------
972 int FILES_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
976 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
979 CONFIGFILE cfstat(fileName, true);
980 int e = cfstat.Error();
984 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
985 errorStr = string("User \'") + login + "\' stat not written\n";
986 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
990 for (int i = 0; i < DIR_NUM; i++)
992 snprintf(s, 22, "D%d", i);
993 cfstat.WriteInt(s, stat.down[i]);
994 snprintf(s, 22, "U%d", i);
995 cfstat.WriteInt(s, stat.up[i]);
998 cfstat.WriteDouble("Cash", stat.cash);
999 cfstat.WriteDouble("FreeMb", stat.freeMb);
1000 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
1001 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1002 cfstat.WriteInt("PassiveTime", stat.passiveTime);
1003 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
1006 int e = chmod(fileName.c_str(), storeSettings.GetStatMode());
1007 e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID());
1011 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1012 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1017 //-----------------------------------------------------------------------------
1018 int FILES_STORE::WriteLogString(const string & str, const string & login) const
1021 time_t tm = time(NULL);
1023 fileName = storeSettings.GetUsersDir() + "/" + login + "/log";
1024 f = fopen(fileName.c_str(), "at");
1028 fprintf(f, "%s", LogDate(tm));
1030 fprintf(f, "%s", str.c_str());
1036 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1037 errorStr = "Cannot open \'" + fileName + "\'";
1038 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1042 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1043 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1047 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1048 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1053 //-----------------------------------------------------------------------------
1054 int FILES_STORE::WriteLog2String(const string & str, const string & login) const
1057 time_t tm = time(NULL);
1059 fileName = storeSettings.GetUsersDir() + "/" + login + "/log2";
1060 f = fopen(fileName.c_str(), "at");
1064 fprintf(f, "%s", LogDate(tm));
1066 fprintf(f, "%s", str.c_str());
1072 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1073 errorStr = "Cannot open \'" + fileName + "\'";
1074 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1078 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1079 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1083 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1084 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1089 //-----------------------------------------------------------------------------
1090 int FILES_STORE::WriteUserChgLog(const string & login,
1091 const string & admLogin,
1093 const string & paramName,
1094 const string & oldValue,
1095 const string & newValue,
1096 const string & message) const
1098 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1099 + paramName + "\' parameter changed from \'" + oldValue +
1100 "\' to \'" + newValue + "\'. " + message;
1102 return WriteLogString(userLogMsg, login);
1104 //-----------------------------------------------------------------------------
1105 int FILES_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1107 string logStr = "Connect, " + inet_ntostring(ip);
1108 if (WriteLogString(logStr, login))
1110 return WriteLog2String(logStr, login);
1112 //-----------------------------------------------------------------------------
1113 int FILES_STORE::WriteUserDisconnect(const string & login,
1114 const DIR_TRAFF & up,
1115 const DIR_TRAFF & down,
1116 const DIR_TRAFF & sessionUp,
1117 const DIR_TRAFF & sessionDown,
1120 const std::string & reason) const
1122 stringstream logStr;
1123 logStr << "Disconnect, "
1124 << " session upload: \'"
1126 << "\' session download: \'"
1128 << "\' month upload: \'"
1130 << "\' month download: \'"
1136 if (WriteLogString(logStr.str(), login))
1139 logStr << " freeMb: \'"
1146 return WriteLog2String(logStr.str(), login);
1148 //-----------------------------------------------------------------------------
1149 int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const
1153 strprintf(&stat1,"%s/%s/stat.%d.%02d",
1154 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1156 CONFIGFILE s(stat1, true);
1160 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1161 errorStr = "Cannot create file '" + stat1 + "'";
1162 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1168 strprintf(&stat2,"%s/%s/stat2.%d.%02d",
1169 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1171 CONFIGFILE s2(stat2, true);
1175 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1176 errorStr = "Cannot create file '" + stat2 + "'";
1177 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1181 for (size_t i = 0; i < DIR_NUM; i++)
1184 snprintf(dirName, 3, "U%llu", (unsigned long long)i);
1185 s.WriteInt(dirName, stat.up[i]); // Classic
1186 s2.WriteInt(dirName, stat.up[i]); // New
1187 snprintf(dirName, 3, "D%llu", (unsigned long long)i);
1188 s.WriteInt(dirName, stat.down[i]); // Classic
1189 s2.WriteInt(dirName, stat.down[i]); // New
1193 s.WriteDouble("cash", stat.cash);
1196 s2.WriteDouble("Cash", stat.cash);
1197 s2.WriteDouble("FreeMb", stat.freeMb);
1198 s2.WriteDouble("LastCashAdd", stat.lastCashAdd);
1199 s2.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1200 s2.WriteInt("PassiveTime", stat.passiveTime);
1201 s2.WriteInt("LastActivityTime", stat.lastActivityTime);
1205 //-----------------------------------------------------------------------------*/
1206 int FILES_STORE::AddAdmin(const string & login) const
1209 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1211 if (Touch(fileName))
1213 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1214 errorStr = "Cannot create file " + fileName;
1215 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1221 //-----------------------------------------------------------------------------*/
1222 int FILES_STORE::DelAdmin(const string & login) const
1225 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1226 if (unlink(fileName.c_str()))
1228 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1229 errorStr = "unlink failed. Message: '";
1230 errorStr += strerror(errno);
1232 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1236 //-----------------------------------------------------------------------------*/
1237 int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1239 char passwordE[2 * ADM_PASSWD_LEN + 2];
1240 char pass[ADM_PASSWD_LEN + 1];
1241 char adminPass[ADM_PASSWD_LEN + 1];
1245 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1248 CONFIGFILE cf(fileName, true);
1254 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1255 errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1256 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1260 memset(pass, 0, sizeof(pass));
1261 memset(adminPass, 0, sizeof(adminPass));
1264 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1266 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1267 adminPass[ADM_PASSWD_LEN - 1] = 0;
1269 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1271 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1274 pass[ADM_PASSWD_LEN - 1] = 0;
1275 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1277 cf.WriteString("password", passwordE);
1278 cf.WriteInt("ChgConf", ac.priv.userConf);
1279 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1280 cf.WriteInt("ChgStat", ac.priv.userStat);
1281 cf.WriteInt("ChgCash", ac.priv.userCash);
1282 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1283 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1284 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1285 cf.WriteInt("ChgService", ac.priv.serviceChg);
1286 cf.WriteInt("ChgCorp", ac.priv.corpChg);
1291 //-----------------------------------------------------------------------------
1292 int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1295 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1296 CONFIGFILE cf(fileName);
1297 char pass[ADM_PASSWD_LEN + 1];
1298 char password[ADM_PASSWD_LEN + 1];
1299 char passwordE[2 * ADM_PASSWD_LEN + 2];
1306 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1307 errorStr = "Cannot open " + fileName;
1308 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1314 if (cf.ReadString("password", &p, "*"))
1316 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1317 errorStr = "Error in parameter password";
1318 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1322 memset(passwordE, 0, sizeof(passwordE));
1323 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1325 memset(pass, 0, sizeof(pass));
1327 if (passwordE[0] != 0)
1329 Decode21(pass, passwordE);
1330 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1332 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1334 DecodeString(password + 8*i, pass + 8*i, &ctx);
1342 ac->password = password;
1344 if (cf.ReadInt("ChgConf", &a, 0) == 0)
1345 ac->priv.userConf = a;
1348 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1349 errorStr = "Error in parameter ChgConf";
1350 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1354 if (cf.ReadInt("ChgPassword", &a, 0) == 0)
1355 ac->priv.userPasswd = a;
1358 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1359 errorStr = "Error in parameter ChgPassword";
1360 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1364 if (cf.ReadInt("ChgStat", &a, 0) == 0)
1365 ac->priv.userStat = a;
1368 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1369 errorStr = "Error in parameter ChgStat";
1370 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1374 if (cf.ReadInt("ChgCash", &a, 0) == 0)
1375 ac->priv.userCash = a;
1378 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1379 errorStr = "Error in parameter ChgCash";
1380 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1384 if (cf.ReadInt("UsrAddDel", &a, 0) == 0)
1385 ac->priv.userAddDel = a;
1388 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1389 errorStr = "Error in parameter UsrAddDel";
1390 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1394 if (cf.ReadInt("ChgAdmin", &a, 0) == 0)
1395 ac->priv.adminChg = a;
1398 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1399 errorStr = "Error in parameter ChgAdmin";
1400 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1404 if (cf.ReadInt("ChgTariff", &a, 0) == 0)
1405 ac->priv.tariffChg = a;
1408 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1409 errorStr = "Error in parameter ChgTariff";
1410 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1414 if (cf.ReadInt("ChgService", &a, 0) == 0)
1415 ac->priv.serviceChg = a;
1417 ac->priv.serviceChg = 0;
1419 if (cf.ReadInt("ChgCorp", &a, 0) == 0)
1420 ac->priv.corpChg = a;
1422 ac->priv.corpChg = 0;
1426 //-----------------------------------------------------------------------------
1427 int FILES_STORE::AddTariff(const string & name) const
1430 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1431 if (Touch(fileName))
1433 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1434 errorStr = "Cannot create file " + fileName;
1435 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1440 //-----------------------------------------------------------------------------
1441 int FILES_STORE::DelTariff(const string & name) const
1444 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1445 if (unlink(fileName.c_str()))
1447 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1448 errorStr = "unlink failed. Message: '";
1449 errorStr += strerror(errno);
1451 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1455 //-----------------------------------------------------------------------------
1456 int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1458 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1459 CONFIGFILE conf(fileName);
1461 td->tariffConf.name = tariffName;
1463 if (conf.Error() != 0)
1465 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1466 errorStr = "Cannot read file " + fileName;
1467 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1472 for (int i = 0; i<DIR_NUM; i++)
1474 strprintf(¶m, "Time%d", i);
1475 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1477 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1478 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1479 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1483 ParseTariffTimeStr(str.c_str(),
1484 td->dirPrice[i].hDay,
1485 td->dirPrice[i].mDay,
1486 td->dirPrice[i].hNight,
1487 td->dirPrice[i].mNight);
1489 strprintf(¶m, "PriceDayA%d", i);
1490 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1492 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1493 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1494 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1497 td->dirPrice[i].priceDayA /= (1024*1024);
1499 strprintf(¶m, "PriceDayB%d", i);
1500 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1502 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1503 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1504 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1507 td->dirPrice[i].priceDayB /= (1024*1024);
1509 strprintf(¶m, "PriceNightA%d", i);
1510 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1512 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1513 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1514 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1517 td->dirPrice[i].priceNightA /= (1024*1024);
1519 strprintf(¶m, "PriceNightB%d", i);
1520 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1522 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1523 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1524 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1527 td->dirPrice[i].priceNightB /= (1024*1024);
1529 strprintf(¶m, "Threshold%d", i);
1530 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1532 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1533 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1534 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1538 strprintf(¶m, "SinglePrice%d", i);
1539 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1541 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1542 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1543 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1547 strprintf(¶m, "NoDiscount%d", i);
1548 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1550 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1551 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1552 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1557 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1559 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1560 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1561 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1565 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1567 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1568 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1569 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1573 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1575 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1576 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1577 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1581 if (conf.ReadString("TraffType", &str, "") < 0)
1583 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1584 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1585 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1589 if (!strcasecmp(str.c_str(), "up"))
1590 td->tariffConf.traffType = TRAFF_UP;
1592 if (!strcasecmp(str.c_str(), "down"))
1593 td->tariffConf.traffType = TRAFF_DOWN;
1595 if (!strcasecmp(str.c_str(), "up+down"))
1596 td->tariffConf.traffType = TRAFF_UP_DOWN;
1598 if (!strcasecmp(str.c_str(), "max"))
1599 td->tariffConf.traffType = TRAFF_MAX;
1602 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1603 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1604 printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str());
1609 //-----------------------------------------------------------------------------
1610 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1612 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1615 CONFIGFILE cf(fileName, true);
1621 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1622 errorStr = "Error writing tariff " + tariffName;
1623 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1628 for (int i = 0; i < DIR_NUM; i++)
1630 strprintf(¶m, "PriceDayA%d", i);
1631 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1633 strprintf(¶m, "PriceDayB%d", i);
1634 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1636 strprintf(¶m, "PriceNightA%d", i);
1637 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1639 strprintf(¶m, "PriceNightB%d", i);
1640 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1642 strprintf(¶m, "Threshold%d", i);
1643 cf.WriteInt(param, td.dirPrice[i].threshold);
1646 strprintf(¶m, "Time%d", i);
1648 strprintf(&s, "%0d:%0d-%0d:%0d",
1649 td.dirPrice[i].hDay,
1650 td.dirPrice[i].mDay,
1651 td.dirPrice[i].hNight,
1652 td.dirPrice[i].mNight);
1654 cf.WriteString(param, s);
1656 strprintf(¶m, "NoDiscount%d", i);
1657 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1659 strprintf(¶m, "SinglePrice%d", i);
1660 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1663 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1664 cf.WriteDouble("Fee", td.tariffConf.fee);
1665 cf.WriteDouble("Free", td.tariffConf.free);
1667 switch (td.tariffConf.traffType)
1670 cf.WriteString("TraffType", "up");
1673 cf.WriteString("TraffType", "down");
1676 cf.WriteString("TraffType", "up+down");
1679 cf.WriteString("TraffType", "max");
1686 //-----------------------------------------------------------------------------
1687 int FILES_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1689 const string & login) const
1691 char fn[FN_STR_LEN];
1692 char dn[FN_STR_LEN];
1699 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1700 if (access(dn, F_OK) != 0)
1702 if (mkdir(dn, 0700) != 0)
1704 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1705 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1706 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1711 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1712 e += chmod(dn, storeSettings.GetStatModeDir());
1716 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1717 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1722 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1728 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1729 storeSettings.GetUsersDir().c_str(),
1733 if (access(dn, F_OK) != 0)
1735 if (mkdir(dn, 0700) != 0)
1737 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1738 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1739 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1744 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1745 e += chmod(dn, storeSettings.GetStatModeDir());
1749 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1750 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1753 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1754 storeSettings.GetUsersDir().c_str(),
1757 lt->tm_mon+1 < 10 ? "0" : "",
1759 if (access(dn, F_OK) != 0)
1761 if (mkdir(dn, 0700) != 0)
1763 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1764 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1765 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1770 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1771 e += chmod(dn, storeSettings.GetStatModeDir());
1775 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1776 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1779 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1781 statFile = fopen (fn, "at");
1785 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1786 errorStr = "File \'" + string(fn) + "\' cannot be written.";
1787 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1794 lt1 = localtime(&lastStat);
1803 lt2 = localtime(&t);
1809 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1810 h1, m1, s1, h2, m2, s2) < 0)
1812 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1813 errorStr = string("fprint failed. Message: '") + strerror(errno) + "'";
1814 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1819 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1820 stIter = statTree.begin();
1822 while (stIter != statTree.end())
1825 x2str(stIter->second.up, u);
1826 x2str(stIter->second.down, d);
1827 #ifdef TRAFF_STAT_WITH_PORTS
1828 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1829 inet_ntostring(stIter->first.ip).c_str(),
1834 stIter->second.cash) < 0)
1836 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1837 errorStr = "fprint failed. Message: '";
1838 errorStr += strerror(errno);
1840 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1845 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1846 inet_ntostring(stIter->first.ip).c_str(),
1850 stIter->second.cash) < 0)
1852 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1853 errorStr = string("fprint failed. Message: '");
1854 errorStr += strerror(errno);
1856 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1867 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1868 e += chmod(fn, storeSettings.GetStatMode());
1872 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1873 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1878 //-----------------------------------------------------------------------------
1879 int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const
1881 //ðÒÏ×ÅÒÉÔØ ÅÓÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1882 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1883 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1889 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1890 if (access(dn.c_str(), F_OK) != 0)
1892 if (mkdir(dn.c_str(), 0700) != 0)
1894 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1895 errorStr = "Directory \'";
1897 errorStr += "\' cannot be created.";
1898 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1903 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1905 gettimeofday(&tv, NULL);
1907 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1908 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1912 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1913 errorStr = "File \'";
1915 errorStr += "\' cannot be writen.";
1916 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1920 return EditMessage(*msg, login);
1922 //-----------------------------------------------------------------------------
1923 int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1925 //ðÒÏ×ÅÒÉÔØ ÅÓÌÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1926 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1927 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1932 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1934 if (access(fileName.c_str(), F_OK) != 0)
1937 x2str(msg.header.id, idstr);
1938 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1939 errorStr = "Message for user \'";
1940 errorStr += login + "\' with ID \'";
1941 errorStr += idstr + "\' does not exist.";
1942 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
1946 Touch(fileName + ".new");
1948 msgFile = fopen((fileName + ".new").c_str(), "wt");
1951 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1952 errorStr = "File \'" + fileName + "\' cannot be writen.";
1953 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1958 res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0);
1959 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1960 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1961 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1962 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1963 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1964 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1968 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1969 errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'";
1970 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
1977 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
1979 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1981 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1982 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1983 printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno));
1989 //-----------------------------------------------------------------------------
1990 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1993 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1994 msg->header.id = id;
1995 return ReadMessage(fn, &msg->header, &msg->text);
1997 //-----------------------------------------------------------------------------
1998 int FILES_STORE::DelMessage(uint64_t id, const string & login) const
2001 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2003 return unlink(fn.c_str());
2005 //-----------------------------------------------------------------------------
2006 int FILES_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
2008 string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/");
2010 //hdrsList->resize(messages.size());
2012 if (access(dn.c_str(), F_OK) != 0)
2017 vector<string> messages;
2018 GetFileList(&messages, dn, S_IFREG, "");
2020 for (unsigned i = 0; i < messages.size(); i++)
2022 unsigned long long id = 0;
2024 if (str2x(messages[i].c_str(), id))
2026 if (unlink((dn + messages[i]).c_str()))
2028 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2029 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2030 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2037 if (ReadMessage(dn + messages[i], &hdr, NULL))
2044 if (unlink((dn + messages[i]).c_str()))
2046 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2047 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2048 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2055 hdrsList->push_back(hdr);
2059 //-----------------------------------------------------------------------------
2060 int FILES_STORE::ReadMessage(const string & fileName,
2062 string * text) const
2065 msgFile = fopen(fileName.c_str(), "rt");
2068 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2069 errorStr = "File \'";
2070 errorStr += fileName;
2071 errorStr += "\' cannot be openned.";
2072 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
2078 d[1] = &hdr->lastSendTime;
2079 d[2] = &hdr->creationTime;
2080 d[3] = &hdr->showTime;
2081 d[4] = (unsigned*)(&hdr->repeat);
2082 d[5] = &hdr->repeatPeriod;
2084 memset(p, 0, sizeof(p));
2086 for (int pos = 0; pos < 6; pos++)
2088 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
2089 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2090 errorStr = "Cannot read file \'";
2091 errorStr += fileName;
2092 errorStr += "\'. Missing data.";
2093 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
2094 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2100 ep = strrchr(p, '\r');
2102 ep = strrchr(p, '\n');
2107 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2108 errorStr = "Cannot read file \'";
2109 errorStr += fileName;
2110 errorStr += "\'. Missing data.";
2111 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
2112 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2117 if (str2x(p, *(d[pos])))
2119 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2120 errorStr = "Cannot read file \'";
2121 errorStr += fileName;
2122 errorStr += "\'. Incorrect value. \'";
2125 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
2132 memset(txt, 0, sizeof(txt));
2135 text->erase(text->begin(), text->end());
2136 while (!feof(msgFile))
2139 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2149 //-----------------------------------------------------------------------------
2150 int FILES_STORE::Touch(const std::string & path) const
2152 FILE * f = fopen(path.c_str(), "wb");
2160 //-----------------------------------------------------------------------------
2161 int GetFileList(vector<string> * fileList, const string & directory, mode_t mode, const string & ext)
2163 // æÕÎËÃÉÑ ÐÒÏÓÍÁÔÒÉ×ÁÅÔ ÓÏÄÅÒÖÉÍÏÅ ÄÉÒÅËÔÏÒÉÉ
2165 DIR * d = opendir(directory.c_str());
2169 printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno));
2174 while ((entry = readdir(d)))
2176 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
2179 string str = directory + "/" + string(entry->d_name);
2182 if (stat(str.c_str(), &st))
2185 if (!(st.st_mode & mode)) // Filter by mode
2191 size_t d_nameLen = strlen(entry->d_name);
2192 if (d_nameLen <= ext.size())
2195 if (ext == entry->d_name + (d_nameLen - ext.size()))
2197 entry->d_name[d_nameLen - ext.size()] = 0;
2198 fileList->push_back(entry->d_name);
2203 fileList->push_back(entry->d_name);
2211 //-----------------------------------------------------------------------------