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"),
345 logger(GetPluginLogger(GetStgLogger(), "store_files"))
347 pthread_mutexattr_t attr;
348 pthread_mutexattr_init(&attr);
349 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
350 pthread_mutex_init(&mutex, &attr);
352 //-----------------------------------------------------------------------------
353 int FILES_STORE::ParseSettings()
355 int ret = storeSettings.ParseSettings(settings);
358 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
359 errorStr = storeSettings.GetStrError();
363 //-----------------------------------------------------------------------------
364 int FILES_STORE::GetUsersList(vector<string> * userList) const
366 vector<string> files;
368 if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, ""))
370 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
371 errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + string(strerror(errno));
375 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
377 userList->swap(files);
381 //-----------------------------------------------------------------------------
382 int FILES_STORE::GetAdminsList(vector<string> * adminList) const
384 vector<string> files;
386 if (GetFileList(&files, storeSettings.GetAdminsDir(), S_IFREG, ".adm"))
388 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
389 errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + string(strerror(errno));
393 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
395 adminList->swap(files);
399 //-----------------------------------------------------------------------------
400 int FILES_STORE::GetTariffsList(vector<string> * tariffList) const
402 vector<string> files;
404 if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf"))
406 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
407 errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + string(strerror(errno));
411 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
413 tariffList->swap(files);
417 //-----------------------------------------------------------------------------
418 int FILES_STORE::RemoveDir(const char * path) const
420 DIR * d = opendir(path);
424 errorStr = "failed to open dir. Message: '";
425 errorStr += strerror(errno);
427 printfd(__FILE__, "FILE_STORE::RemoveDir() - Failed to open dir '%s': '%s'\n", path, strerror(errno));
432 while ((entry = readdir(d)))
434 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
438 str += "/" + string(entry->d_name);
441 if (stat(str.c_str(), &st))
444 if ((st.st_mode & S_IFREG))
446 if (unlink(str.c_str()))
448 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
449 errorStr = "unlink failed. Message: '";
450 errorStr += strerror(errno);
452 printfd(__FILE__, "FILES_STORE::RemoveDir() - unlink failed. Message: '%s'\n", strerror(errno));
458 if (!(st.st_mode & S_IFDIR))
460 if (RemoveDir(str.c_str()))
473 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
474 errorStr = "rmdir failed. Message: '";
475 errorStr += strerror(errno);
477 printfd(__FILE__, "FILES_STORE::RemoveDir() - rmdir failed. Message: '%s'\n", strerror(errno));
483 //-----------------------------------------------------------------------------
484 int FILES_STORE::AddUser(const string & login) const
488 strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str());
490 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
492 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
493 errorStr = string("mkdir failed. Message: '") + strerror(errno) + "'";
494 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
498 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
501 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
502 errorStr = "Cannot create file \"" + fileName + "\'";
503 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
507 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
510 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
511 errorStr = "Cannot create file \"" + fileName + "\'";
512 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
517 //-----------------------------------------------------------------------------
518 int FILES_STORE::DelUser(const string & login) const
523 strprintf(&dirName, "%s/"DELETED_USERS_DIR, storeSettings.GetWorkDir().c_str());
524 if (access(dirName.c_str(), F_OK) != 0)
526 if (mkdir(dirName.c_str(), 0700) != 0)
528 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
529 errorStr = "Directory '" + dirName + "' cannot be created.";
530 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
535 if (access(dirName.c_str(), F_OK) == 0)
537 strprintf(&dirName, "%s/"DELETED_USERS_DIR"/%s.%lu", storeSettings.GetWorkDir().c_str(), login.c_str(), time(NULL));
538 strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
539 if (rename(dirName1.c_str(), dirName.c_str()))
541 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
542 errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
543 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
549 strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
550 if (RemoveDir(dirName.c_str()))
557 //-----------------------------------------------------------------------------
558 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
561 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
562 if (RestoreUserConf(conf, login, fileName))
564 if (!storeSettings.GetReadBak())
568 return RestoreUserConf(conf, login, fileName + ".bak");
572 //-----------------------------------------------------------------------------
573 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login, const string & fileName) const
575 CONFIGFILE cf(fileName);
581 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
582 errorStr = "User \'" + login + "\' data not read.";
583 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
587 if (cf.ReadString("Password", &conf->password, "") < 0)
589 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
590 errorStr = "User \'" + login + "\' data not read. Parameter Password.";
591 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
594 if (conf->password.empty())
596 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
597 errorStr = "User \'" + login + "\' password is blank.";
598 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
602 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
604 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
605 errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
606 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
609 if (conf->tariffName.empty())
611 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
612 errorStr = "User \'" + login + "\' tariff is blank.";
613 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
618 cf.ReadString("IP", &ipStr, "?");
624 catch (const string & s)
626 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
627 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
628 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
633 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
635 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
636 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
637 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
641 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
643 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
644 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
645 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
649 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
651 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
652 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
653 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
657 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
658 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
659 cf.ReadString("TariffChange", &conf->nextTariff, "");
660 cf.ReadString("Group", &conf->group, "");
661 cf.ReadString("RealName", &conf->realName, "");
662 cf.ReadString("Address", &conf->address, "");
663 cf.ReadString("Phone", &conf->phone, "");
664 cf.ReadString("Note", &conf->note, "");
665 cf.ReadString("email", &conf->email, "");
667 char userdataName[12];
668 for (int i = 0; i < USERDATA_NUM; i++)
670 snprintf(userdataName, 12, "Userdata%d", i);
671 cf.ReadString(userdataName, &conf->userdata[i], "");
674 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
676 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
677 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
678 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
684 //-----------------------------------------------------------------------------
685 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
688 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
690 if (RestoreUserStat(stat, login, fileName))
692 if (!storeSettings.GetReadBak())
696 return RestoreUserStat(stat, login, fileName + ".bak");
700 //-----------------------------------------------------------------------------
701 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login, const string & fileName) const
703 CONFIGFILE cf(fileName);
709 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
710 errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
711 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
717 for (int i = 0; i < DIR_NUM; i++)
720 snprintf(s, 22, "D%d", i);
721 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
723 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
724 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
725 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
728 stat->down[i] = traff;
730 snprintf(s, 22, "U%d", i);
731 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
733 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
734 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
735 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
741 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
743 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
744 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
745 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
749 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
751 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
752 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
753 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
757 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
759 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
760 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
761 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
765 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
767 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
768 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
769 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
773 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
775 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
776 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
777 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
781 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
783 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
784 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
785 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
791 //-----------------------------------------------------------------------------
792 int FILES_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
795 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
797 CONFIGFILE cfstat(fileName, true);
799 int e = cfstat.Error();
803 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
804 errorStr = string("User \'") + login + "\' conf not written\n";
805 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
809 e = chmod(fileName.c_str(), storeSettings.GetConfMode());
810 e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID());
814 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
815 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
818 cfstat.WriteString("Password", conf.password);
819 cfstat.WriteInt ("Passive", conf.passive);
820 cfstat.WriteInt ("Down", conf.disabled);
821 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
822 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
823 cfstat.WriteString("Tariff", conf.tariffName);
824 cfstat.WriteString("Address", conf.address);
825 cfstat.WriteString("Phone", conf.phone);
826 cfstat.WriteString("Email", conf.email);
827 cfstat.WriteString("Note", conf.note);
828 cfstat.WriteString("RealName", conf.realName);
829 cfstat.WriteString("Group", conf.group);
830 cfstat.WriteDouble("Credit", conf.credit);
831 cfstat.WriteString("TariffChange", conf.nextTariff);
833 char userdataName[12];
834 for (int i = 0; i < USERDATA_NUM; i++)
836 snprintf(userdataName, 12, "Userdata%d", i);
837 cfstat.WriteString(userdataName, conf.userdata[i]);
839 cfstat.WriteInt("CreditExpire", conf.creditExpire);
843 cfstat.WriteString("IP", ipStr.str());
847 //-----------------------------------------------------------------------------
848 int FILES_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
852 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
855 CONFIGFILE cfstat(fileName, true);
856 int e = cfstat.Error();
860 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
861 errorStr = string("User \'") + login + "\' stat not written\n";
862 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
866 for (int i = 0; i < DIR_NUM; i++)
868 snprintf(s, 22, "D%d", i);
869 cfstat.WriteInt(s, stat.down[i]);
870 snprintf(s, 22, "U%d", i);
871 cfstat.WriteInt(s, stat.up[i]);
874 cfstat.WriteDouble("Cash", stat.cash);
875 cfstat.WriteDouble("FreeMb", stat.freeMb);
876 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
877 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
878 cfstat.WriteInt("PassiveTime", stat.passiveTime);
879 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
882 int e = chmod(fileName.c_str(), storeSettings.GetStatMode());
883 e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID());
887 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
888 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
893 //-----------------------------------------------------------------------------
894 int FILES_STORE::WriteLogString(const string & str, const string & login) const
897 time_t tm = time(NULL);
899 fileName = storeSettings.GetUsersDir() + "/" + login + "/log";
900 f = fopen(fileName.c_str(), "at");
904 fprintf(f, "%s", LogDate(tm));
906 fprintf(f, "%s", str.c_str());
912 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
913 errorStr = "Cannot open \'" + fileName + "\'";
914 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
918 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
919 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
923 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
924 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
929 //-----------------------------------------------------------------------------
930 int FILES_STORE::WriteLog2String(const string & str, const string & login) const
933 time_t tm = time(NULL);
935 fileName = storeSettings.GetUsersDir() + "/" + login + "/log2";
936 f = fopen(fileName.c_str(), "at");
940 fprintf(f, "%s", LogDate(tm));
942 fprintf(f, "%s", str.c_str());
948 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
949 errorStr = "Cannot open \'" + fileName + "\'";
950 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
954 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
955 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
959 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
960 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
965 //-----------------------------------------------------------------------------
966 int FILES_STORE::WriteUserChgLog(const string & login,
967 const string & admLogin,
969 const string & paramName,
970 const string & oldValue,
971 const string & newValue,
972 const string & message) const
974 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
975 + paramName + "\' parameter changed from \'" + oldValue +
976 "\' to \'" + newValue + "\'. " + message;
978 return WriteLogString(userLogMsg, login);
980 //-----------------------------------------------------------------------------
981 int FILES_STORE::WriteUserConnect(const string & login, uint32_t ip) const
983 string logStr = "Connect, " + inet_ntostring(ip);
984 if (WriteLogString(logStr, login))
986 return WriteLog2String(logStr, login);
988 //-----------------------------------------------------------------------------
989 int FILES_STORE::WriteUserDisconnect(const string & login,
990 const DIR_TRAFF & up,
991 const DIR_TRAFF & down,
992 const DIR_TRAFF & sessionUp,
993 const DIR_TRAFF & sessionDown,
996 const std::string & reason) const
999 logStr << "Disconnect, "
1000 << " session upload: \'"
1002 << "\' session download: \'"
1004 << "\' month upload: \'"
1006 << "\' month download: \'"
1012 if (WriteLogString(logStr.str(), login))
1015 logStr << " freeMb: \'"
1022 return WriteLog2String(logStr.str(), login);
1024 //-----------------------------------------------------------------------------
1025 int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const
1029 strprintf(&stat1,"%s/%s/stat.%d.%02d",
1030 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1032 CONFIGFILE s(stat1, true);
1036 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1037 errorStr = "Cannot create file '" + stat1 + "'";
1038 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1044 strprintf(&stat2,"%s/%s/stat2.%d.%02d",
1045 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1047 CONFIGFILE s2(stat2, true);
1051 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1052 errorStr = "Cannot create file '" + stat2 + "'";
1053 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1057 for (size_t i = 0; i < DIR_NUM; i++)
1060 snprintf(dirName, 3, "U%llu", (unsigned long long)i);
1061 s.WriteInt(dirName, stat.up[i]); // Classic
1062 s2.WriteInt(dirName, stat.up[i]); // New
1063 snprintf(dirName, 3, "D%llu", (unsigned long long)i);
1064 s.WriteInt(dirName, stat.down[i]); // Classic
1065 s2.WriteInt(dirName, stat.down[i]); // New
1069 s.WriteDouble("cash", stat.cash);
1072 s2.WriteDouble("Cash", stat.cash);
1073 s2.WriteDouble("FreeMb", stat.freeMb);
1074 s2.WriteDouble("LastCashAdd", stat.lastCashAdd);
1075 s2.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1076 s2.WriteInt("PassiveTime", stat.passiveTime);
1077 s2.WriteInt("LastActivityTime", stat.lastActivityTime);
1081 //-----------------------------------------------------------------------------*/
1082 int FILES_STORE::AddAdmin(const string & login) const
1085 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1087 if (Touch(fileName))
1089 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1090 errorStr = "Cannot create file " + fileName;
1091 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1097 //-----------------------------------------------------------------------------*/
1098 int FILES_STORE::DelAdmin(const string & login) const
1101 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1102 if (unlink(fileName.c_str()))
1104 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1105 errorStr = "unlink failed. Message: '";
1106 errorStr += strerror(errno);
1108 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1112 //-----------------------------------------------------------------------------*/
1113 int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1115 char passwordE[2 * ADM_PASSWD_LEN + 2];
1116 char pass[ADM_PASSWD_LEN + 1];
1117 char adminPass[ADM_PASSWD_LEN + 1];
1121 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1124 CONFIGFILE cf(fileName, true);
1130 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1131 errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1132 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1136 memset(pass, 0, sizeof(pass));
1137 memset(adminPass, 0, sizeof(adminPass));
1140 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1142 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1143 adminPass[ADM_PASSWD_LEN - 1] = 0;
1145 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1147 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1150 pass[ADM_PASSWD_LEN - 1] = 0;
1151 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1153 cf.WriteString("password", passwordE);
1154 cf.WriteInt("ChgConf", ac.priv.userConf);
1155 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1156 cf.WriteInt("ChgStat", ac.priv.userStat);
1157 cf.WriteInt("ChgCash", ac.priv.userCash);
1158 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1159 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1160 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1161 cf.WriteInt("ChgService", ac.priv.serviceChg);
1162 cf.WriteInt("ChgCorp", ac.priv.corpChg);
1167 //-----------------------------------------------------------------------------
1168 int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1171 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1172 CONFIGFILE cf(fileName);
1173 char pass[ADM_PASSWD_LEN + 1];
1174 char password[ADM_PASSWD_LEN + 1];
1175 char passwordE[2 * ADM_PASSWD_LEN + 2];
1182 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1183 errorStr = "Cannot open " + fileName;
1184 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1190 if (cf.ReadString("password", &p, "*"))
1192 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1193 errorStr = "Error in parameter password";
1194 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1198 memset(passwordE, 0, sizeof(passwordE));
1199 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1201 memset(pass, 0, sizeof(pass));
1203 if (passwordE[0] != 0)
1205 Decode21(pass, passwordE);
1206 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1208 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1210 DecodeString(password + 8*i, pass + 8*i, &ctx);
1218 ac->password = password;
1220 if (cf.ReadInt("ChgConf", &a, 0) == 0)
1221 ac->priv.userConf = a;
1224 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1225 errorStr = "Error in parameter ChgConf";
1226 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1230 if (cf.ReadInt("ChgPassword", &a, 0) == 0)
1231 ac->priv.userPasswd = a;
1234 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1235 errorStr = "Error in parameter ChgPassword";
1236 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1240 if (cf.ReadInt("ChgStat", &a, 0) == 0)
1241 ac->priv.userStat = a;
1244 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1245 errorStr = "Error in parameter ChgStat";
1246 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1250 if (cf.ReadInt("ChgCash", &a, 0) == 0)
1251 ac->priv.userCash = a;
1254 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1255 errorStr = "Error in parameter ChgCash";
1256 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1260 if (cf.ReadInt("UsrAddDel", &a, 0) == 0)
1261 ac->priv.userAddDel = a;
1264 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1265 errorStr = "Error in parameter UsrAddDel";
1266 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1270 if (cf.ReadInt("ChgAdmin", &a, 0) == 0)
1271 ac->priv.adminChg = a;
1274 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1275 errorStr = "Error in parameter ChgAdmin";
1276 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1280 if (cf.ReadInt("ChgTariff", &a, 0) == 0)
1281 ac->priv.tariffChg = a;
1284 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1285 errorStr = "Error in parameter ChgTariff";
1286 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1290 if (cf.ReadInt("ChgService", &a, 0) == 0)
1291 ac->priv.serviceChg = a;
1293 ac->priv.serviceChg = 0;
1295 if (cf.ReadInt("ChgCorp", &a, 0) == 0)
1296 ac->priv.corpChg = a;
1298 ac->priv.corpChg = 0;
1302 //-----------------------------------------------------------------------------
1303 int FILES_STORE::AddTariff(const string & name) const
1306 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1307 if (Touch(fileName))
1309 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1310 errorStr = "Cannot create file " + fileName;
1311 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1316 //-----------------------------------------------------------------------------
1317 int FILES_STORE::DelTariff(const string & name) const
1320 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1321 if (unlink(fileName.c_str()))
1323 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1324 errorStr = "unlink failed. Message: '";
1325 errorStr += strerror(errno);
1327 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1331 //-----------------------------------------------------------------------------
1332 int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1334 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1335 CONFIGFILE conf(fileName);
1337 td->tariffConf.name = tariffName;
1339 if (conf.Error() != 0)
1341 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1342 errorStr = "Cannot read file " + fileName;
1343 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1348 for (int i = 0; i<DIR_NUM; i++)
1350 strprintf(¶m, "Time%d", i);
1351 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1353 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1354 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1355 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1359 ParseTariffTimeStr(str.c_str(),
1360 td->dirPrice[i].hDay,
1361 td->dirPrice[i].mDay,
1362 td->dirPrice[i].hNight,
1363 td->dirPrice[i].mNight);
1365 strprintf(¶m, "PriceDayA%d", i);
1366 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1368 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1369 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1370 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1373 td->dirPrice[i].priceDayA /= (1024*1024);
1375 strprintf(¶m, "PriceDayB%d", i);
1376 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1378 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1379 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1380 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1383 td->dirPrice[i].priceDayB /= (1024*1024);
1385 strprintf(¶m, "PriceNightA%d", i);
1386 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1388 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1389 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1390 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1393 td->dirPrice[i].priceNightA /= (1024*1024);
1395 strprintf(¶m, "PriceNightB%d", i);
1396 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1398 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1399 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1400 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1403 td->dirPrice[i].priceNightB /= (1024*1024);
1405 strprintf(¶m, "Threshold%d", i);
1406 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1408 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1409 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1410 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1414 strprintf(¶m, "SinglePrice%d", i);
1415 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1417 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1418 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1419 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1423 strprintf(¶m, "NoDiscount%d", i);
1424 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1426 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1427 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1428 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1433 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1435 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1436 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1437 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1441 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1443 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1444 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1445 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1449 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1451 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1452 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1453 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1457 if (conf.ReadString("TraffType", &str, "") < 0)
1459 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1460 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1461 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1465 if (!strcasecmp(str.c_str(), "up"))
1466 td->tariffConf.traffType = TRAFF_UP;
1468 if (!strcasecmp(str.c_str(), "down"))
1469 td->tariffConf.traffType = TRAFF_DOWN;
1471 if (!strcasecmp(str.c_str(), "up+down"))
1472 td->tariffConf.traffType = TRAFF_UP_DOWN;
1474 if (!strcasecmp(str.c_str(), "max"))
1475 td->tariffConf.traffType = TRAFF_MAX;
1478 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1479 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1480 printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str());
1485 //-----------------------------------------------------------------------------
1486 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1488 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1491 CONFIGFILE cf(fileName, true);
1497 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1498 errorStr = "Error writing tariff " + tariffName;
1499 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1504 for (int i = 0; i < DIR_NUM; i++)
1506 strprintf(¶m, "PriceDayA%d", i);
1507 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1509 strprintf(¶m, "PriceDayB%d", i);
1510 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1512 strprintf(¶m, "PriceNightA%d", i);
1513 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1515 strprintf(¶m, "PriceNightB%d", i);
1516 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1518 strprintf(¶m, "Threshold%d", i);
1519 cf.WriteInt(param, td.dirPrice[i].threshold);
1522 strprintf(¶m, "Time%d", i);
1524 strprintf(&s, "%0d:%0d-%0d:%0d",
1525 td.dirPrice[i].hDay,
1526 td.dirPrice[i].mDay,
1527 td.dirPrice[i].hNight,
1528 td.dirPrice[i].mNight);
1530 cf.WriteString(param, s);
1532 strprintf(¶m, "NoDiscount%d", i);
1533 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1535 strprintf(¶m, "SinglePrice%d", i);
1536 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1539 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1540 cf.WriteDouble("Fee", td.tariffConf.fee);
1541 cf.WriteDouble("Free", td.tariffConf.free);
1543 switch (td.tariffConf.traffType)
1546 cf.WriteString("TraffType", "up");
1549 cf.WriteString("TraffType", "down");
1552 cf.WriteString("TraffType", "up+down");
1555 cf.WriteString("TraffType", "max");
1562 //-----------------------------------------------------------------------------
1563 int FILES_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1565 const string & login) const
1567 char fn[FN_STR_LEN];
1568 char dn[FN_STR_LEN];
1575 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1576 if (access(dn, F_OK) != 0)
1578 if (mkdir(dn, 0700) != 0)
1580 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1581 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1582 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1587 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1588 e += chmod(dn, storeSettings.GetStatModeDir());
1592 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1593 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1598 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1604 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1605 storeSettings.GetUsersDir().c_str(),
1609 if (access(dn, F_OK) != 0)
1611 if (mkdir(dn, 0700) != 0)
1613 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1614 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1615 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1620 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1621 e += chmod(dn, storeSettings.GetStatModeDir());
1625 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1626 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1629 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1630 storeSettings.GetUsersDir().c_str(),
1633 lt->tm_mon+1 < 10 ? "0" : "",
1635 if (access(dn, F_OK) != 0)
1637 if (mkdir(dn, 0700) != 0)
1639 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1640 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1641 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1646 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1647 e += chmod(dn, storeSettings.GetStatModeDir());
1651 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1652 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1655 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1657 statFile = fopen (fn, "at");
1661 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1662 errorStr = "File \'" + string(fn) + "\' cannot be written.";
1663 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1670 lt1 = localtime(&lastStat);
1679 lt2 = localtime(&t);
1685 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1686 h1, m1, s1, h2, m2, s2) < 0)
1688 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1689 errorStr = string("fprint failed. Message: '") + strerror(errno) + "'";
1690 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1695 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1696 stIter = statTree.begin();
1698 while (stIter != statTree.end())
1701 x2str(stIter->second.up, u);
1702 x2str(stIter->second.down, d);
1703 #ifdef TRAFF_STAT_WITH_PORTS
1704 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1705 inet_ntostring(stIter->first.ip).c_str(),
1710 stIter->second.cash) < 0)
1712 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1713 errorStr = "fprint failed. Message: '";
1714 errorStr += strerror(errno);
1716 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1721 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1722 inet_ntostring(stIter->first.ip).c_str(),
1726 stIter->second.cash) < 0)
1728 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1729 errorStr = string("fprint failed. Message: '");
1730 errorStr += strerror(errno);
1732 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1743 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1744 e += chmod(fn, storeSettings.GetStatMode());
1748 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1749 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1754 //-----------------------------------------------------------------------------
1755 int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const
1761 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1762 if (access(dn.c_str(), F_OK) != 0)
1764 if (mkdir(dn.c_str(), 0700) != 0)
1766 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1767 errorStr = "Directory \'";
1769 errorStr += "\' cannot be created.";
1770 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1775 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1777 gettimeofday(&tv, NULL);
1779 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1780 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1784 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1785 errorStr = "File \'";
1787 errorStr += "\' cannot be writen.";
1788 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1792 return EditMessage(*msg, login);
1794 //-----------------------------------------------------------------------------
1795 int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1800 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1802 if (access(fileName.c_str(), F_OK) != 0)
1805 x2str(msg.header.id, idstr);
1806 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1807 errorStr = "Message for user \'";
1808 errorStr += login + "\' with ID \'";
1809 errorStr += idstr + "\' does not exist.";
1810 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
1814 Touch(fileName + ".new");
1816 msgFile = fopen((fileName + ".new").c_str(), "wt");
1819 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1820 errorStr = "File \'" + fileName + "\' cannot be writen.";
1821 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1826 res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0);
1827 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1828 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1829 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1830 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1831 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1832 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1836 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1837 errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'";
1838 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
1845 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
1847 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1849 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1850 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1851 printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno));
1857 //-----------------------------------------------------------------------------
1858 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1861 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1862 msg->header.id = id;
1863 return ReadMessage(fn, &msg->header, &msg->text);
1865 //-----------------------------------------------------------------------------
1866 int FILES_STORE::DelMessage(uint64_t id, const string & login) const
1869 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1871 return unlink(fn.c_str());
1873 //-----------------------------------------------------------------------------
1874 int FILES_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
1876 string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/");
1878 if (access(dn.c_str(), F_OK) != 0)
1883 vector<string> messages;
1884 GetFileList(&messages, dn, S_IFREG, "");
1886 for (unsigned i = 0; i < messages.size(); i++)
1888 unsigned long long id = 0;
1890 if (str2x(messages[i].c_str(), id))
1892 if (unlink((dn + messages[i]).c_str()))
1894 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1895 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
1896 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
1903 if (ReadMessage(dn + messages[i], &hdr, NULL))
1910 if (unlink((dn + messages[i]).c_str()))
1912 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1913 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
1914 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
1921 hdrsList->push_back(hdr);
1925 //-----------------------------------------------------------------------------
1926 int FILES_STORE::ReadMessage(const string & fileName,
1928 string * text) const
1931 msgFile = fopen(fileName.c_str(), "rt");
1934 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1935 errorStr = "File \'";
1936 errorStr += fileName;
1937 errorStr += "\' cannot be openned.";
1938 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
1944 d[1] = &hdr->lastSendTime;
1945 d[2] = &hdr->creationTime;
1946 d[3] = &hdr->showTime;
1947 d[4] = (unsigned*)(&hdr->repeat);
1948 d[5] = &hdr->repeatPeriod;
1950 memset(p, 0, sizeof(p));
1952 for (int pos = 0; pos < 6; pos++)
1954 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
1955 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1956 errorStr = "Cannot read file \'";
1957 errorStr += fileName;
1958 errorStr += "\'. Missing data.";
1959 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
1960 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
1966 ep = strrchr(p, '\r');
1968 ep = strrchr(p, '\n');
1973 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1974 errorStr = "Cannot read file \'";
1975 errorStr += fileName;
1976 errorStr += "\'. Missing data.";
1977 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
1978 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
1983 if (str2x(p, *(d[pos])))
1985 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1986 errorStr = "Cannot read file \'";
1987 errorStr += fileName;
1988 errorStr += "\'. Incorrect value. \'";
1991 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
1998 memset(txt, 0, sizeof(txt));
2001 text->erase(text->begin(), text->end());
2002 while (!feof(msgFile))
2005 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2015 //-----------------------------------------------------------------------------
2016 int FILES_STORE::Touch(const std::string & path) const
2018 FILE * f = fopen(path.c_str(), "wb");
2026 //-----------------------------------------------------------------------------
2027 int GetFileList(vector<string> * fileList, const string & directory, mode_t mode, const string & ext)
2029 DIR * d = opendir(directory.c_str());
2033 printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno));
2038 while ((entry = readdir(d)))
2040 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
2043 string str = directory + "/" + string(entry->d_name);
2046 if (stat(str.c_str(), &st))
2049 if (!(st.st_mode & mode)) // Filter by mode
2055 size_t d_nameLen = strlen(entry->d_name);
2056 if (d_nameLen <= ext.size())
2059 if (ext == entry->d_name + (d_nameLen - ext.size()))
2061 entry->d_name[d_nameLen - ext.size()] = 0;
2062 fileList->push_back(entry->d_name);
2067 fileList->push_back(entry->d_name);
2075 //-----------------------------------------------------------------------------