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());
1483 if (conf.ReadString("Period", &str, "month") < 0)
1484 td->tariffConf.period = TARIFF::MONTH;
1486 td->tariffConf.period = TARIFF::StringToPeriod(str);
1489 //-----------------------------------------------------------------------------
1490 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1492 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1495 CONFIGFILE cf(fileName, true);
1501 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1502 errorStr = "Error writing tariff " + tariffName;
1503 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1508 for (int i = 0; i < DIR_NUM; i++)
1510 strprintf(¶m, "PriceDayA%d", i);
1511 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1513 strprintf(¶m, "PriceDayB%d", i);
1514 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1516 strprintf(¶m, "PriceNightA%d", i);
1517 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1519 strprintf(¶m, "PriceNightB%d", i);
1520 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1522 strprintf(¶m, "Threshold%d", i);
1523 cf.WriteInt(param, td.dirPrice[i].threshold);
1526 strprintf(¶m, "Time%d", i);
1528 strprintf(&s, "%0d:%0d-%0d:%0d",
1529 td.dirPrice[i].hDay,
1530 td.dirPrice[i].mDay,
1531 td.dirPrice[i].hNight,
1532 td.dirPrice[i].mNight);
1534 cf.WriteString(param, s);
1536 strprintf(¶m, "NoDiscount%d", i);
1537 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1539 strprintf(¶m, "SinglePrice%d", i);
1540 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1543 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1544 cf.WriteDouble("Fee", td.tariffConf.fee);
1545 cf.WriteDouble("Free", td.tariffConf.free);
1547 switch (td.tariffConf.traffType)
1550 cf.WriteString("TraffType", "up");
1553 cf.WriteString("TraffType", "down");
1556 cf.WriteString("TraffType", "up+down");
1559 cf.WriteString("TraffType", "max");
1563 cf.WriteString("Period", TARIFF::PeriodToString(td.tariffConf.period));
1568 //-----------------------------------------------------------------------------
1569 int FILES_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1571 const string & login) const
1573 char fn[FN_STR_LEN];
1574 char dn[FN_STR_LEN];
1581 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1582 if (access(dn, F_OK) != 0)
1584 if (mkdir(dn, 0700) != 0)
1586 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1587 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1588 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1593 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1594 e += chmod(dn, storeSettings.GetStatModeDir());
1598 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1599 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1604 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1610 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1611 storeSettings.GetUsersDir().c_str(),
1615 if (access(dn, F_OK) != 0)
1617 if (mkdir(dn, 0700) != 0)
1619 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1620 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1621 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1626 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1627 e += chmod(dn, storeSettings.GetStatModeDir());
1631 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1632 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1635 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1636 storeSettings.GetUsersDir().c_str(),
1639 lt->tm_mon+1 < 10 ? "0" : "",
1641 if (access(dn, F_OK) != 0)
1643 if (mkdir(dn, 0700) != 0)
1645 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1646 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1647 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1652 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1653 e += chmod(dn, storeSettings.GetStatModeDir());
1657 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1658 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1661 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1663 statFile = fopen (fn, "at");
1667 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1668 errorStr = "File \'" + string(fn) + "\' cannot be written.";
1669 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1676 lt1 = localtime(&lastStat);
1685 lt2 = localtime(&t);
1691 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1692 h1, m1, s1, h2, m2, s2) < 0)
1694 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1695 errorStr = string("fprint failed. Message: '") + strerror(errno) + "'";
1696 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1701 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1702 stIter = statTree.begin();
1704 while (stIter != statTree.end())
1707 x2str(stIter->second.up, u);
1708 x2str(stIter->second.down, d);
1709 #ifdef TRAFF_STAT_WITH_PORTS
1710 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1711 inet_ntostring(stIter->first.ip).c_str(),
1716 stIter->second.cash) < 0)
1718 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1719 errorStr = "fprint failed. Message: '";
1720 errorStr += strerror(errno);
1722 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1727 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1728 inet_ntostring(stIter->first.ip).c_str(),
1732 stIter->second.cash) < 0)
1734 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1735 errorStr = string("fprint failed. Message: '");
1736 errorStr += strerror(errno);
1738 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1749 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1750 e += chmod(fn, storeSettings.GetStatMode());
1754 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1755 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1760 //-----------------------------------------------------------------------------
1761 int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const
1767 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1768 if (access(dn.c_str(), F_OK) != 0)
1770 if (mkdir(dn.c_str(), 0700) != 0)
1772 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1773 errorStr = "Directory \'";
1775 errorStr += "\' cannot be created.";
1776 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1781 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1783 gettimeofday(&tv, NULL);
1785 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1786 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1790 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1791 errorStr = "File \'";
1793 errorStr += "\' cannot be writen.";
1794 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1798 return EditMessage(*msg, login);
1800 //-----------------------------------------------------------------------------
1801 int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1806 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1808 if (access(fileName.c_str(), F_OK) != 0)
1811 x2str(msg.header.id, idstr);
1812 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1813 errorStr = "Message for user \'";
1814 errorStr += login + "\' with ID \'";
1815 errorStr += idstr + "\' does not exist.";
1816 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
1820 Touch(fileName + ".new");
1822 msgFile = fopen((fileName + ".new").c_str(), "wt");
1825 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1826 errorStr = "File \'" + fileName + "\' cannot be writen.";
1827 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1832 res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0);
1833 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1834 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1835 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1836 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1837 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1838 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1842 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1843 errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'";
1844 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
1851 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
1853 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1855 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1856 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1857 printfd(__FILE__, "FILES_STORE::EditMessage - rename failed. Message: '%s'\n", strerror(errno));
1863 //-----------------------------------------------------------------------------
1864 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1867 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1868 msg->header.id = id;
1869 return ReadMessage(fn, &msg->header, &msg->text);
1871 //-----------------------------------------------------------------------------
1872 int FILES_STORE::DelMessage(uint64_t id, const string & login) const
1875 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1877 return unlink(fn.c_str());
1879 //-----------------------------------------------------------------------------
1880 int FILES_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
1882 string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/");
1884 if (access(dn.c_str(), F_OK) != 0)
1889 vector<string> messages;
1890 GetFileList(&messages, dn, S_IFREG, "");
1892 for (unsigned i = 0; i < messages.size(); i++)
1894 unsigned long long id = 0;
1896 if (str2x(messages[i].c_str(), id))
1898 if (unlink((dn + messages[i]).c_str()))
1900 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1901 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
1902 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
1909 if (ReadMessage(dn + messages[i], &hdr, NULL))
1916 if (unlink((dn + messages[i]).c_str()))
1918 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1919 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
1920 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
1927 hdrsList->push_back(hdr);
1931 //-----------------------------------------------------------------------------
1932 int FILES_STORE::ReadMessage(const string & fileName,
1934 string * text) const
1937 msgFile = fopen(fileName.c_str(), "rt");
1940 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1941 errorStr = "File \'";
1942 errorStr += fileName;
1943 errorStr += "\' cannot be openned.";
1944 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
1950 d[1] = &hdr->lastSendTime;
1951 d[2] = &hdr->creationTime;
1952 d[3] = &hdr->showTime;
1953 d[4] = (unsigned*)(&hdr->repeat);
1954 d[5] = &hdr->repeatPeriod;
1956 memset(p, 0, sizeof(p));
1958 for (int pos = 0; pos < 6; pos++)
1960 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
1961 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1962 errorStr = "Cannot read file \'";
1963 errorStr += fileName;
1964 errorStr += "\'. Missing data.";
1965 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
1966 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
1972 ep = strrchr(p, '\r');
1974 ep = strrchr(p, '\n');
1979 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1980 errorStr = "Cannot read file \'";
1981 errorStr += fileName;
1982 errorStr += "\'. Missing data.";
1983 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
1984 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
1989 if (str2x(p, *(d[pos])))
1991 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1992 errorStr = "Cannot read file \'";
1993 errorStr += fileName;
1994 errorStr += "\'. Incorrect value. \'";
1997 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
2004 memset(txt, 0, sizeof(txt));
2007 text->erase(text->begin(), text->end());
2008 while (!feof(msgFile))
2011 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2021 //-----------------------------------------------------------------------------
2022 int FILES_STORE::Touch(const std::string & path) const
2024 FILE * f = fopen(path.c_str(), "wb");
2032 //-----------------------------------------------------------------------------
2033 int GetFileList(vector<string> * fileList, const string & directory, mode_t mode, const string & ext)
2035 DIR * d = opendir(directory.c_str());
2039 printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno));
2044 while ((entry = readdir(d)))
2046 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
2049 string str = directory + "/" + string(entry->d_name);
2052 if (stat(str.c_str(), &st))
2055 if (!(st.st_mode & mode)) // Filter by mode
2061 size_t d_nameLen = strlen(entry->d_name);
2062 if (d_nameLen <= ext.size())
2065 if (ext == entry->d_name + (d_nameLen - ext.size()))
2067 entry->d_name[d_nameLen - ext.size()] = 0;
2068 fileList->push_back(entry->d_name);
2073 fileList->push_back(entry->d_name);
2081 //-----------------------------------------------------------------------------