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()
98 //-----------------------------------------------------------------------------
99 int FILES_STORE_SETTINGS::ParseOwner(const vector<PARAM_VALUE> & moduleParams, const string & owner, uid_t * uid)
103 vector<PARAM_VALUE>::const_iterator pvi;
104 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
105 if (pvi == moduleParams.end())
107 errorStr = "Parameter \'" + owner + "\' not found.";
108 printfd(__FILE__, "%s\n", errorStr.c_str());
111 if (User2UID(pvi->value[0].c_str(), uid) < 0)
113 errorStr = "Parameter \'" + owner + "\': Unknown user \'" + pvi->value[0] + "\'";
114 printfd(__FILE__, "%s\n", errorStr.c_str());
119 //-----------------------------------------------------------------------------
120 int FILES_STORE_SETTINGS::ParseGroup(const vector<PARAM_VALUE> & moduleParams, const string & group, gid_t * gid)
124 vector<PARAM_VALUE>::const_iterator pvi;
125 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
126 if (pvi == moduleParams.end())
128 errorStr = "Parameter \'" + group + "\' not found.";
129 printfd(__FILE__, "%s\n", errorStr.c_str());
132 if (Group2GID(pvi->value[0].c_str(), gid) < 0)
134 errorStr = "Parameter \'" + group + "\': Unknown group \'" + pvi->value[0] + "\'";
135 printfd(__FILE__, "%s\n", errorStr.c_str());
140 //-----------------------------------------------------------------------------
141 int FILES_STORE_SETTINGS::ParseYesNo(const string & value, bool * val)
143 if (0 == strcasecmp(value.c_str(), "yes"))
148 if (0 == strcasecmp(value.c_str(), "no"))
154 errorStr = "Incorrect value \'" + value + "\'.";
157 //-----------------------------------------------------------------------------
158 int FILES_STORE_SETTINGS::ParseMode(const vector<PARAM_VALUE> & moduleParams, const string & modeStr, mode_t * mode)
162 vector<PARAM_VALUE>::const_iterator pvi;
163 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
164 if (pvi == moduleParams.end())
166 errorStr = "Parameter \'" + modeStr + "\' not found.";
167 printfd(__FILE__, "%s\n", errorStr.c_str());
170 if (Str2Mode(pvi->value[0].c_str(), mode) < 0)
172 errorStr = "Parameter \'" + modeStr + "\': Incorrect mode \'" + pvi->value[0] + "\'";
173 printfd(__FILE__, "%s\n", errorStr.c_str());
178 //-----------------------------------------------------------------------------
179 int FILES_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
181 if (ParseOwner(s.moduleParams, "StatOwner", &statUID) < 0)
183 if (ParseGroup(s.moduleParams, "StatGroup", &statGID) < 0)
185 if (ParseMode(s.moduleParams, "StatMode", &statMode) < 0)
188 if (ParseOwner(s.moduleParams, "ConfOwner", &confUID) < 0)
190 if (ParseGroup(s.moduleParams, "ConfGroup", &confGID) < 0)
192 if (ParseMode(s.moduleParams, "ConfMode", &confMode) < 0)
195 if (ParseOwner(s.moduleParams, "UserLogOwner", &userLogUID) < 0)
197 if (ParseGroup(s.moduleParams, "UserLogGroup", &userLogGID) < 0)
199 if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0)
202 vector<PARAM_VALUE>::const_iterator pvi;
204 pv.param = "RemoveBak";
205 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
206 if (pvi == s.moduleParams.end())
212 if (ParseYesNo(pvi->value[0], &removeBak))
214 printfd(__FILE__, "Cannot parse parameter 'RemoveBak'\n");
219 pv.param = "ReadBak";
220 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
221 if (pvi == s.moduleParams.end())
227 if (ParseYesNo(pvi->value[0], &readBak))
229 printfd(__FILE__, "Cannot parse parameter 'ReadBak'\n");
234 pv.param = "WorkDir";
235 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
236 if (pvi == s.moduleParams.end())
238 errorStr = "Parameter \'WorkDir\' not found.";
239 printfd(__FILE__, "Parameter 'WorkDir' not found\n");
243 workDir = pvi->value[0];
244 if (workDir.size() && workDir[workDir.size() - 1] == '/')
246 workDir.resize(workDir.size() - 1);
248 usersDir = workDir + "/users/";
249 tariffsDir = workDir + "/tariffs/";
250 adminsDir = workDir + "/admins/";
254 //-----------------------------------------------------------------------------
255 const string & FILES_STORE_SETTINGS::GetStrError() const
259 //-----------------------------------------------------------------------------
260 int FILES_STORE_SETTINGS::User2UID(const char * user, uid_t * uid)
266 errorStr = string("User \'") + string(user) + string("\' not found in system.");
267 printfd(__FILE__, "%s\n", errorStr.c_str());
274 //-----------------------------------------------------------------------------
275 int FILES_STORE_SETTINGS::Group2GID(const char * gr, gid_t * gid)
281 errorStr = string("Group \'") + string(gr) + string("\' not found in system.");
282 printfd(__FILE__, "%s\n", errorStr.c_str());
289 //-----------------------------------------------------------------------------
290 int FILES_STORE_SETTINGS::Str2Mode(const char * str, mode_t * mode)
297 errorStr = string("Error parsing mode \'") + str + string("\'");
298 printfd(__FILE__, "%s\n", errorStr.c_str());
302 for (int i = 0; i < 3; i++)
303 if (str[i] > '7' || str[i] < '0')
305 errorStr = string("Error parsing mode \'") + str + string("\'");
306 printfd(__FILE__, "%s\n", errorStr.c_str());
314 *mode = ((mode_t)c) + ((mode_t)b << 3) + ((mode_t)a << 6);
318 //-----------------------------------------------------------------------------
319 mode_t FILES_STORE_SETTINGS::GetStatModeDir() const
321 mode_t mode = statMode;
322 if (statMode & S_IRUSR) mode |= S_IXUSR;
323 if (statMode & S_IRGRP) mode |= S_IXGRP;
324 if (statMode & S_IROTH) mode |= S_IXOTH;
327 //-----------------------------------------------------------------------------
328 mode_t FILES_STORE_SETTINGS::GetConfModeDir() const
330 mode_t mode = confMode;
331 if (confMode & S_IRUSR) mode |= S_IXUSR;
332 if (confMode & S_IRGRP) mode |= S_IXGRP;
333 if (confMode & S_IROTH) mode |= S_IXOTH;
336 //-----------------------------------------------------------------------------
337 //-----------------------------------------------------------------------------
338 //-----------------------------------------------------------------------------
339 FILES_STORE::FILES_STORE()
341 version("file_store v.1.04"),
346 pthread_mutexattr_t attr;
347 pthread_mutexattr_init(&attr);
348 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
349 pthread_mutex_init(&mutex, &attr);
351 //-----------------------------------------------------------------------------
352 int FILES_STORE::ParseSettings()
354 int ret = storeSettings.ParseSettings(settings);
357 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
358 errorStr = storeSettings.GetStrError();
362 //-----------------------------------------------------------------------------
363 int FILES_STORE::GetUsersList(vector<string> * userList) const
365 vector<string> files;
367 if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, ""))
369 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
370 errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + string(strerror(errno));
374 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
376 userList->swap(files);
380 //-----------------------------------------------------------------------------
381 int FILES_STORE::GetAdminsList(vector<string> * adminList) const
383 vector<string> files;
385 if (GetFileList(&files, storeSettings.GetAdminsDir(), S_IFREG, ".adm"))
387 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
388 errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + string(strerror(errno));
392 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
394 adminList->swap(files);
398 //-----------------------------------------------------------------------------
399 int FILES_STORE::GetTariffsList(vector<string> * tariffList) const
401 vector<string> files;
403 if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf"))
405 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
406 errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + string(strerror(errno));
410 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
412 tariffList->swap(files);
416 //-----------------------------------------------------------------------------
417 int FILES_STORE::RemoveDir(const char * path) const
419 DIR * d = opendir(path);
423 errorStr = "failed to open dir. Message: '";
424 errorStr += strerror(errno);
426 printfd(__FILE__, "FILE_STORE::RemoveDir() - Failed to open dir '%s': '%s'\n", path, strerror(errno));
431 while ((entry = readdir(d)))
433 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
437 str += "/" + string(entry->d_name);
440 if (stat(str.c_str(), &st))
443 if ((st.st_mode & S_IFREG))
445 if (unlink(str.c_str()))
447 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
448 errorStr = "unlink failed. Message: '";
449 errorStr += strerror(errno);
451 printfd(__FILE__, "FILES_STORE::RemoveDir() - unlink failed. Message: '%s'\n", strerror(errno));
457 if (!(st.st_mode & S_IFDIR))
459 if (RemoveDir(str.c_str()))
472 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
473 errorStr = "rmdir failed. Message: '";
474 errorStr += strerror(errno);
476 printfd(__FILE__, "FILES_STORE::RemoveDir() - rmdir failed. Message: '%s'\n", strerror(errno));
482 //-----------------------------------------------------------------------------
483 int FILES_STORE::AddUser(const string & login) const
487 strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str());
489 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
491 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
492 errorStr = string("mkdir failed. Message: '") + strerror(errno) + "'";
493 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
497 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
500 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
501 errorStr = "Cannot create file \"" + fileName + "\'";
502 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
506 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
509 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
510 errorStr = "Cannot create file \"" + fileName + "\'";
511 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
516 //-----------------------------------------------------------------------------
517 int FILES_STORE::DelUser(const string & login) const
522 strprintf(&dirName, "%s/"DELETED_USERS_DIR, storeSettings.GetWorkDir().c_str());
523 if (access(dirName.c_str(), F_OK) != 0)
525 if (mkdir(dirName.c_str(), 0700) != 0)
527 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
528 errorStr = "Directory '" + dirName + "' cannot be created.";
529 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
534 if (access(dirName.c_str(), F_OK) == 0)
536 strprintf(&dirName, "%s/"DELETED_USERS_DIR"/%s.%lu", storeSettings.GetWorkDir().c_str(), login.c_str(), time(NULL));
537 strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
538 if (rename(dirName1.c_str(), dirName.c_str()))
540 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
541 errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
542 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
548 strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
549 if (RemoveDir(dirName.c_str()))
556 //-----------------------------------------------------------------------------
557 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
560 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
561 if (RestoreUserConf(conf, login, fileName))
563 if (!storeSettings.GetReadBak())
567 return RestoreUserConf(conf, login, fileName + ".bak");
571 //-----------------------------------------------------------------------------
572 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login, const string & fileName) const
574 CONFIGFILE cf(fileName);
580 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
581 errorStr = "User \'" + login + "\' data not read.";
582 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
586 if (cf.ReadString("Password", &conf->password, "") < 0)
588 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
589 errorStr = "User \'" + login + "\' data not read. Parameter Password.";
590 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
593 if (conf->password.empty())
595 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
596 errorStr = "User \'" + login + "\' password is blank.";
597 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
601 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
603 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
604 errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
605 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
608 if (conf->tariffName.empty())
610 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
611 errorStr = "User \'" + login + "\' tariff is blank.";
612 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
617 cf.ReadString("IP", &ipStr, "?");
623 catch (const string & s)
625 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
626 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
627 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
632 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
634 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
635 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
636 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
640 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
642 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
643 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
644 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
648 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
650 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
651 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
652 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
656 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
657 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
658 cf.ReadString("TariffChange", &conf->nextTariff, "");
659 cf.ReadString("Group", &conf->group, "");
660 cf.ReadString("RealName", &conf->realName, "");
661 cf.ReadString("Address", &conf->address, "");
662 cf.ReadString("Phone", &conf->phone, "");
663 cf.ReadString("Note", &conf->note, "");
664 cf.ReadString("email", &conf->email, "");
666 char userdataName[12];
667 for (int i = 0; i < USERDATA_NUM; i++)
669 snprintf(userdataName, 12, "Userdata%d", i);
670 cf.ReadString(userdataName, &conf->userdata[i], "");
673 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
675 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
676 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
677 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
683 //-----------------------------------------------------------------------------
684 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
687 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
689 if (RestoreUserStat(stat, login, fileName))
691 if (!storeSettings.GetReadBak())
695 return RestoreUserStat(stat, login, fileName + ".bak");
699 //-----------------------------------------------------------------------------
700 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login, const string & fileName) const
702 CONFIGFILE cf(fileName);
708 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
709 errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
710 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
716 for (int i = 0; i < DIR_NUM; i++)
719 snprintf(s, 22, "D%d", i);
720 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
722 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
723 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
724 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
727 stat->down[i] = traff;
729 snprintf(s, 22, "U%d", i);
730 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
732 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
733 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
734 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
740 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
742 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
743 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
744 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
748 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
750 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
751 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
752 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
756 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
758 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
759 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
760 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
764 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
766 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
767 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
768 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
772 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
774 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
775 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
776 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
780 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
782 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
783 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
784 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
790 //-----------------------------------------------------------------------------
791 int FILES_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
794 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
796 CONFIGFILE cfstat(fileName, true);
798 int e = cfstat.Error();
802 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
803 errorStr = string("User \'") + login + "\' conf not written\n";
804 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
808 e = chmod(fileName.c_str(), storeSettings.GetConfMode());
809 e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID());
813 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
814 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
817 cfstat.WriteString("Password", conf.password);
818 cfstat.WriteInt ("Passive", conf.passive);
819 cfstat.WriteInt ("Down", conf.disabled);
820 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
821 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
822 cfstat.WriteString("Tariff", conf.tariffName);
823 cfstat.WriteString("Address", conf.address);
824 cfstat.WriteString("Phone", conf.phone);
825 cfstat.WriteString("Email", conf.email);
826 cfstat.WriteString("Note", conf.note);
827 cfstat.WriteString("RealName", conf.realName);
828 cfstat.WriteString("Group", conf.group);
829 cfstat.WriteDouble("Credit", conf.credit);
830 cfstat.WriteString("TariffChange", conf.nextTariff);
832 char userdataName[12];
833 for (int i = 0; i < USERDATA_NUM; i++)
835 snprintf(userdataName, 12, "Userdata%d", i);
836 cfstat.WriteString(userdataName, conf.userdata[i]);
838 cfstat.WriteInt("CreditExpire", conf.creditExpire);
842 cfstat.WriteString("IP", ipStr.str());
846 //-----------------------------------------------------------------------------
847 int FILES_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
851 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
854 CONFIGFILE cfstat(fileName, true);
855 int e = cfstat.Error();
859 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
860 errorStr = string("User \'") + login + "\' stat not written\n";
861 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
865 for (int i = 0; i < DIR_NUM; i++)
867 snprintf(s, 22, "D%d", i);
868 cfstat.WriteInt(s, stat.down[i]);
869 snprintf(s, 22, "U%d", i);
870 cfstat.WriteInt(s, stat.up[i]);
873 cfstat.WriteDouble("Cash", stat.cash);
874 cfstat.WriteDouble("FreeMb", stat.freeMb);
875 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
876 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
877 cfstat.WriteInt("PassiveTime", stat.passiveTime);
878 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
881 int e = chmod(fileName.c_str(), storeSettings.GetStatMode());
882 e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID());
886 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
887 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
892 //-----------------------------------------------------------------------------
893 int FILES_STORE::WriteLogString(const string & str, const string & login) const
896 time_t tm = time(NULL);
898 fileName = storeSettings.GetUsersDir() + "/" + login + "/log";
899 f = fopen(fileName.c_str(), "at");
903 fprintf(f, "%s", LogDate(tm));
905 fprintf(f, "%s", str.c_str());
911 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
912 errorStr = "Cannot open \'" + fileName + "\'";
913 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
917 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
918 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
922 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
923 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
928 //-----------------------------------------------------------------------------
929 int FILES_STORE::WriteLog2String(const string & str, const string & login) const
932 time_t tm = time(NULL);
934 fileName = storeSettings.GetUsersDir() + "/" + login + "/log2";
935 f = fopen(fileName.c_str(), "at");
939 fprintf(f, "%s", LogDate(tm));
941 fprintf(f, "%s", str.c_str());
947 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
948 errorStr = "Cannot open \'" + fileName + "\'";
949 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
953 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
954 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
958 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
959 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
964 //-----------------------------------------------------------------------------
965 int FILES_STORE::WriteUserChgLog(const string & login,
966 const string & admLogin,
968 const string & paramName,
969 const string & oldValue,
970 const string & newValue,
971 const string & message) const
973 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
974 + paramName + "\' parameter changed from \'" + oldValue +
975 "\' to \'" + newValue + "\'. " + message;
977 return WriteLogString(userLogMsg, login);
979 //-----------------------------------------------------------------------------
980 int FILES_STORE::WriteUserConnect(const string & login, uint32_t ip) const
982 string logStr = "Connect, " + inet_ntostring(ip);
983 if (WriteLogString(logStr, login))
985 return WriteLog2String(logStr, login);
987 //-----------------------------------------------------------------------------
988 int FILES_STORE::WriteUserDisconnect(const string & login,
989 const DIR_TRAFF & up,
990 const DIR_TRAFF & down,
991 const DIR_TRAFF & sessionUp,
992 const DIR_TRAFF & sessionDown,
995 const std::string & reason) const
998 logStr << "Disconnect, "
999 << " session upload: \'"
1001 << "\' session download: \'"
1003 << "\' month upload: \'"
1005 << "\' month download: \'"
1011 if (WriteLogString(logStr.str(), login))
1014 logStr << " freeMb: \'"
1021 return WriteLog2String(logStr.str(), login);
1023 //-----------------------------------------------------------------------------
1024 int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const
1028 strprintf(&stat1,"%s/%s/stat.%d.%02d",
1029 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1031 CONFIGFILE s(stat1, true);
1035 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1036 errorStr = "Cannot create file '" + stat1 + "'";
1037 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1043 strprintf(&stat2,"%s/%s/stat2.%d.%02d",
1044 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1046 CONFIGFILE s2(stat2, true);
1050 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1051 errorStr = "Cannot create file '" + stat2 + "'";
1052 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1056 for (size_t i = 0; i < DIR_NUM; i++)
1059 snprintf(dirName, 3, "U%llu", (unsigned long long)i);
1060 s.WriteInt(dirName, stat.up[i]); // Classic
1061 s2.WriteInt(dirName, stat.up[i]); // New
1062 snprintf(dirName, 3, "D%llu", (unsigned long long)i);
1063 s.WriteInt(dirName, stat.down[i]); // Classic
1064 s2.WriteInt(dirName, stat.down[i]); // New
1068 s.WriteDouble("cash", stat.cash);
1071 s2.WriteDouble("Cash", stat.cash);
1072 s2.WriteDouble("FreeMb", stat.freeMb);
1073 s2.WriteDouble("LastCashAdd", stat.lastCashAdd);
1074 s2.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1075 s2.WriteInt("PassiveTime", stat.passiveTime);
1076 s2.WriteInt("LastActivityTime", stat.lastActivityTime);
1080 //-----------------------------------------------------------------------------*/
1081 int FILES_STORE::AddAdmin(const string & login) const
1084 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1086 if (Touch(fileName))
1088 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1089 errorStr = "Cannot create file " + fileName;
1090 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1096 //-----------------------------------------------------------------------------*/
1097 int FILES_STORE::DelAdmin(const string & login) const
1100 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1101 if (unlink(fileName.c_str()))
1103 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1104 errorStr = "unlink failed. Message: '";
1105 errorStr += strerror(errno);
1107 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1111 //-----------------------------------------------------------------------------*/
1112 int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1114 char passwordE[2 * ADM_PASSWD_LEN + 2];
1115 char pass[ADM_PASSWD_LEN + 1];
1116 char adminPass[ADM_PASSWD_LEN + 1];
1120 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1123 CONFIGFILE cf(fileName, true);
1129 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1130 errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1131 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1135 memset(pass, 0, sizeof(pass));
1136 memset(adminPass, 0, sizeof(adminPass));
1139 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1141 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1142 adminPass[ADM_PASSWD_LEN - 1] = 0;
1144 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1146 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1149 pass[ADM_PASSWD_LEN - 1] = 0;
1150 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1152 cf.WriteString("password", passwordE);
1153 cf.WriteInt("ChgConf", ac.priv.userConf);
1154 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1155 cf.WriteInt("ChgStat", ac.priv.userStat);
1156 cf.WriteInt("ChgCash", ac.priv.userCash);
1157 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1158 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1159 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1160 cf.WriteInt("ChgService", ac.priv.serviceChg);
1161 cf.WriteInt("ChgCorp", ac.priv.corpChg);
1166 //-----------------------------------------------------------------------------
1167 int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1170 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1171 CONFIGFILE cf(fileName);
1172 char pass[ADM_PASSWD_LEN + 1];
1173 char password[ADM_PASSWD_LEN + 1];
1174 char passwordE[2 * ADM_PASSWD_LEN + 2];
1181 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1182 errorStr = "Cannot open " + fileName;
1183 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1189 if (cf.ReadString("password", &p, "*"))
1191 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1192 errorStr = "Error in parameter password";
1193 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1197 memset(passwordE, 0, sizeof(passwordE));
1198 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1200 memset(pass, 0, sizeof(pass));
1202 if (passwordE[0] != 0)
1204 Decode21(pass, passwordE);
1205 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1207 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1209 DecodeString(password + 8*i, pass + 8*i, &ctx);
1217 ac->password = password;
1219 if (cf.ReadInt("ChgConf", &a, 0) == 0)
1220 ac->priv.userConf = a;
1223 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1224 errorStr = "Error in parameter ChgConf";
1225 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1229 if (cf.ReadInt("ChgPassword", &a, 0) == 0)
1230 ac->priv.userPasswd = a;
1233 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1234 errorStr = "Error in parameter ChgPassword";
1235 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1239 if (cf.ReadInt("ChgStat", &a, 0) == 0)
1240 ac->priv.userStat = a;
1243 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1244 errorStr = "Error in parameter ChgStat";
1245 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1249 if (cf.ReadInt("ChgCash", &a, 0) == 0)
1250 ac->priv.userCash = a;
1253 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1254 errorStr = "Error in parameter ChgCash";
1255 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1259 if (cf.ReadInt("UsrAddDel", &a, 0) == 0)
1260 ac->priv.userAddDel = a;
1263 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1264 errorStr = "Error in parameter UsrAddDel";
1265 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1269 if (cf.ReadInt("ChgAdmin", &a, 0) == 0)
1270 ac->priv.adminChg = a;
1273 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1274 errorStr = "Error in parameter ChgAdmin";
1275 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1279 if (cf.ReadInt("ChgTariff", &a, 0) == 0)
1280 ac->priv.tariffChg = a;
1283 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1284 errorStr = "Error in parameter ChgTariff";
1285 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1289 if (cf.ReadInt("ChgService", &a, 0) == 0)
1290 ac->priv.serviceChg = a;
1292 ac->priv.serviceChg = 0;
1294 if (cf.ReadInt("ChgCorp", &a, 0) == 0)
1295 ac->priv.corpChg = a;
1297 ac->priv.corpChg = 0;
1301 //-----------------------------------------------------------------------------
1302 int FILES_STORE::AddTariff(const string & name) const
1305 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1306 if (Touch(fileName))
1308 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1309 errorStr = "Cannot create file " + fileName;
1310 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1315 //-----------------------------------------------------------------------------
1316 int FILES_STORE::DelTariff(const string & name) const
1319 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1320 if (unlink(fileName.c_str()))
1322 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1323 errorStr = "unlink failed. Message: '";
1324 errorStr += strerror(errno);
1326 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1330 //-----------------------------------------------------------------------------
1331 int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1333 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1334 CONFIGFILE conf(fileName);
1336 td->tariffConf.name = tariffName;
1338 if (conf.Error() != 0)
1340 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1341 errorStr = "Cannot read file " + fileName;
1342 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1347 for (int i = 0; i<DIR_NUM; i++)
1349 strprintf(¶m, "Time%d", i);
1350 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1352 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1353 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1354 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1358 ParseTariffTimeStr(str.c_str(),
1359 td->dirPrice[i].hDay,
1360 td->dirPrice[i].mDay,
1361 td->dirPrice[i].hNight,
1362 td->dirPrice[i].mNight);
1364 strprintf(¶m, "PriceDayA%d", i);
1365 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1367 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1368 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1369 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1372 td->dirPrice[i].priceDayA /= (1024*1024);
1374 strprintf(¶m, "PriceDayB%d", i);
1375 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1377 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1378 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1379 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1382 td->dirPrice[i].priceDayB /= (1024*1024);
1384 strprintf(¶m, "PriceNightA%d", i);
1385 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1387 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1388 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1389 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1392 td->dirPrice[i].priceNightA /= (1024*1024);
1394 strprintf(¶m, "PriceNightB%d", i);
1395 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1397 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1398 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1399 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1402 td->dirPrice[i].priceNightB /= (1024*1024);
1404 strprintf(¶m, "Threshold%d", i);
1405 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1407 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1408 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1409 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1413 strprintf(¶m, "SinglePrice%d", i);
1414 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1416 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1417 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1418 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1422 strprintf(¶m, "NoDiscount%d", i);
1423 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1425 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1426 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1427 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1432 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1434 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1435 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1436 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1440 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1442 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1443 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1444 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1448 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1450 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1451 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1452 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1456 if (conf.ReadString("TraffType", &str, "") < 0)
1458 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1459 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1460 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1464 if (!strcasecmp(str.c_str(), "up"))
1465 td->tariffConf.traffType = TRAFF_UP;
1467 if (!strcasecmp(str.c_str(), "down"))
1468 td->tariffConf.traffType = TRAFF_DOWN;
1470 if (!strcasecmp(str.c_str(), "up+down"))
1471 td->tariffConf.traffType = TRAFF_UP_DOWN;
1473 if (!strcasecmp(str.c_str(), "max"))
1474 td->tariffConf.traffType = TRAFF_MAX;
1477 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1478 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1479 printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str());
1484 //-----------------------------------------------------------------------------
1485 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1487 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1490 CONFIGFILE cf(fileName, true);
1496 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1497 errorStr = "Error writing tariff " + tariffName;
1498 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1503 for (int i = 0; i < DIR_NUM; i++)
1505 strprintf(¶m, "PriceDayA%d", i);
1506 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1508 strprintf(¶m, "PriceDayB%d", i);
1509 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1511 strprintf(¶m, "PriceNightA%d", i);
1512 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1514 strprintf(¶m, "PriceNightB%d", i);
1515 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1517 strprintf(¶m, "Threshold%d", i);
1518 cf.WriteInt(param, td.dirPrice[i].threshold);
1521 strprintf(¶m, "Time%d", i);
1523 strprintf(&s, "%0d:%0d-%0d:%0d",
1524 td.dirPrice[i].hDay,
1525 td.dirPrice[i].mDay,
1526 td.dirPrice[i].hNight,
1527 td.dirPrice[i].mNight);
1529 cf.WriteString(param, s);
1531 strprintf(¶m, "NoDiscount%d", i);
1532 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1534 strprintf(¶m, "SinglePrice%d", i);
1535 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1538 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1539 cf.WriteDouble("Fee", td.tariffConf.fee);
1540 cf.WriteDouble("Free", td.tariffConf.free);
1542 switch (td.tariffConf.traffType)
1545 cf.WriteString("TraffType", "up");
1548 cf.WriteString("TraffType", "down");
1551 cf.WriteString("TraffType", "up+down");
1554 cf.WriteString("TraffType", "max");
1561 //-----------------------------------------------------------------------------
1562 int FILES_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1564 const string & login) const
1566 char fn[FN_STR_LEN];
1567 char dn[FN_STR_LEN];
1574 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1575 if (access(dn, F_OK) != 0)
1577 if (mkdir(dn, 0700) != 0)
1579 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1580 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1581 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1586 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1587 e += chmod(dn, storeSettings.GetStatModeDir());
1591 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1592 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1597 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1603 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1604 storeSettings.GetUsersDir().c_str(),
1608 if (access(dn, F_OK) != 0)
1610 if (mkdir(dn, 0700) != 0)
1612 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1613 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1614 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1619 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1620 e += chmod(dn, storeSettings.GetStatModeDir());
1624 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1625 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1628 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1629 storeSettings.GetUsersDir().c_str(),
1632 lt->tm_mon+1 < 10 ? "0" : "",
1634 if (access(dn, F_OK) != 0)
1636 if (mkdir(dn, 0700) != 0)
1638 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1639 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1640 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1645 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1646 e += chmod(dn, storeSettings.GetStatModeDir());
1650 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1651 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1654 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1656 statFile = fopen (fn, "at");
1660 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1661 errorStr = "File \'" + string(fn) + "\' cannot be written.";
1662 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1669 lt1 = localtime(&lastStat);
1678 lt2 = localtime(&t);
1684 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1685 h1, m1, s1, h2, m2, s2) < 0)
1687 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1688 errorStr = string("fprint failed. Message: '") + strerror(errno) + "'";
1689 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1694 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1695 stIter = statTree.begin();
1697 while (stIter != statTree.end())
1700 x2str(stIter->second.up, u);
1701 x2str(stIter->second.down, d);
1702 #ifdef TRAFF_STAT_WITH_PORTS
1703 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1704 inet_ntostring(stIter->first.ip).c_str(),
1709 stIter->second.cash) < 0)
1711 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1712 errorStr = "fprint failed. Message: '";
1713 errorStr += strerror(errno);
1715 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1720 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1721 inet_ntostring(stIter->first.ip).c_str(),
1725 stIter->second.cash) < 0)
1727 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1728 errorStr = string("fprint failed. Message: '");
1729 errorStr += strerror(errno);
1731 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1742 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1743 e += chmod(fn, storeSettings.GetStatMode());
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));
1753 //-----------------------------------------------------------------------------
1754 int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const
1760 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1761 if (access(dn.c_str(), F_OK) != 0)
1763 if (mkdir(dn.c_str(), 0700) != 0)
1765 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1766 errorStr = "Directory \'";
1768 errorStr += "\' cannot be created.";
1769 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1774 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1776 gettimeofday(&tv, NULL);
1778 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1779 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1783 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1784 errorStr = "File \'";
1786 errorStr += "\' cannot be writen.";
1787 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1791 return EditMessage(*msg, login);
1793 //-----------------------------------------------------------------------------
1794 int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1799 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1801 if (access(fileName.c_str(), F_OK) != 0)
1804 x2str(msg.header.id, idstr);
1805 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1806 errorStr = "Message for user \'";
1807 errorStr += login + "\' with ID \'";
1808 errorStr += idstr + "\' does not exist.";
1809 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
1813 Touch(fileName + ".new");
1815 msgFile = fopen((fileName + ".new").c_str(), "wt");
1818 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1819 errorStr = "File \'" + fileName + "\' cannot be writen.";
1820 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1825 res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0);
1826 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1827 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1828 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1829 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1830 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1831 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1835 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1836 errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'";
1837 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
1844 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
1846 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1848 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1849 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1850 printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno));
1856 //-----------------------------------------------------------------------------
1857 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1860 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1861 msg->header.id = id;
1862 return ReadMessage(fn, &msg->header, &msg->text);
1864 //-----------------------------------------------------------------------------
1865 int FILES_STORE::DelMessage(uint64_t id, const string & login) const
1868 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1870 return unlink(fn.c_str());
1872 //-----------------------------------------------------------------------------
1873 int FILES_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
1875 string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/");
1877 if (access(dn.c_str(), F_OK) != 0)
1882 vector<string> messages;
1883 GetFileList(&messages, dn, S_IFREG, "");
1885 for (unsigned i = 0; i < messages.size(); i++)
1887 unsigned long long id = 0;
1889 if (str2x(messages[i].c_str(), id))
1891 if (unlink((dn + messages[i]).c_str()))
1893 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1894 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
1895 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
1902 if (ReadMessage(dn + messages[i], &hdr, NULL))
1909 if (unlink((dn + messages[i]).c_str()))
1911 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1912 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
1913 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
1920 hdrsList->push_back(hdr);
1924 //-----------------------------------------------------------------------------
1925 int FILES_STORE::ReadMessage(const string & fileName,
1927 string * text) const
1930 msgFile = fopen(fileName.c_str(), "rt");
1933 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1934 errorStr = "File \'";
1935 errorStr += fileName;
1936 errorStr += "\' cannot be openned.";
1937 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
1943 d[1] = &hdr->lastSendTime;
1944 d[2] = &hdr->creationTime;
1945 d[3] = &hdr->showTime;
1946 d[4] = (unsigned*)(&hdr->repeat);
1947 d[5] = &hdr->repeatPeriod;
1949 memset(p, 0, sizeof(p));
1951 for (int pos = 0; pos < 6; pos++)
1953 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
1954 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1955 errorStr = "Cannot read file \'";
1956 errorStr += fileName;
1957 errorStr += "\'. Missing data.";
1958 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
1959 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
1965 ep = strrchr(p, '\r');
1967 ep = strrchr(p, '\n');
1972 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1973 errorStr = "Cannot read file \'";
1974 errorStr += fileName;
1975 errorStr += "\'. Missing data.";
1976 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
1977 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
1982 if (str2x(p, *(d[pos])))
1984 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1985 errorStr = "Cannot read file \'";
1986 errorStr += fileName;
1987 errorStr += "\'. Incorrect value. \'";
1990 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
1997 memset(txt, 0, sizeof(txt));
2000 text->erase(text->begin(), text->end());
2001 while (!feof(msgFile))
2004 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2014 //-----------------------------------------------------------------------------
2015 int FILES_STORE::Touch(const std::string & path) const
2017 FILE * f = fopen(path.c_str(), "wb");
2025 //-----------------------------------------------------------------------------
2026 int GetFileList(vector<string> * fileList, const string & directory, mode_t mode, const string & ext)
2028 DIR * d = opendir(directory.c_str());
2032 printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno));
2037 while ((entry = readdir(d)))
2039 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
2042 string str = directory + "/" + string(entry->d_name);
2045 if (stat(str.c_str(), &st))
2048 if (!(st.st_mode & mode)) // Filter by mode
2054 size_t d_nameLen = strlen(entry->d_name);
2055 if (d_nameLen <= ext.size())
2058 if (ext == entry->d_name + (d_nameLen - ext.size()))
2060 entry->d_name[d_nameLen - ext.size()] = 0;
2061 fileList->push_back(entry->d_name);
2066 fileList->push_back(entry->d_name);
2074 //-----------------------------------------------------------------------------