]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/store/files/file_store.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / projects / stargazer / plugins / store / files / file_store.cpp
index 4e4489adcaa86614d32aaa0b390e24b5e513506b..4c95aa31da4bb9b30e19ba31e34393a5a0c8da0f 100644 (file)
@@ -67,6 +67,16 @@ const int pt_mega = 1024 * 1024;
 namespace
 {
 PLUGIN_CREATOR<FILES_STORE> fsc;
+
+bool CheckAndCreate(const std::string & dir, mode_t mode)
+{
+if (access(dir.c_str(), F_OK) == 0)
+    return true;
+if (mkdir(dir.c_str(), mode) == 0)
+    return true;
+return false;
+}
+
 }
 
 extern "C" STORE * GetStore();
@@ -80,11 +90,6 @@ return fsc.GetPlugin();
 //-----------------------------------------------------------------------------
 FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
     : settings(NULL),
-      errorStr(),
-      workDir(),
-      usersDir(),
-      adminsDir(),
-      tariffsDir(),
       statMode(0),
       statUID(0),
       statGID(0),
@@ -105,7 +110,7 @@ PARAM_VALUE pv;
 pv.param = owner;
 std::vector<PARAM_VALUE>::const_iterator pvi;
 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
-if (pvi == moduleParams.end())
+if (pvi == moduleParams.end() || pvi->value.empty())
     {
     errorStr = "Parameter \'" + owner + "\' not found.";
     printfd(__FILE__, "%s\n", errorStr.c_str());
@@ -126,7 +131,7 @@ PARAM_VALUE pv;
 pv.param = group;
 std::vector<PARAM_VALUE>::const_iterator pvi;
 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
-if (pvi == moduleParams.end())
+if (pvi == moduleParams.end() || pvi->value.empty())
     {
     errorStr = "Parameter \'" + group + "\' not found.";
     printfd(__FILE__, "%s\n", errorStr.c_str());
@@ -164,7 +169,7 @@ PARAM_VALUE pv;
 pv.param = modeStr;
 std::vector<PARAM_VALUE>::const_iterator pvi;
 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
-if (pvi == moduleParams.end())
+if (pvi == moduleParams.end() || pvi->value.empty())
     {
     errorStr = "Parameter \'" + modeStr + "\' not found.";
     printfd(__FILE__, "%s\n", errorStr.c_str());
@@ -206,7 +211,7 @@ std::vector<PARAM_VALUE>::const_iterator pvi;
 PARAM_VALUE pv;
 pv.param = "RemoveBak";
 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
-if (pvi == s.moduleParams.end())
+if (pvi == s.moduleParams.end() || pvi->value.empty())
     {
     removeBak = true;
     }
@@ -221,7 +226,7 @@ else
 
 pv.param = "ReadBak";
 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
-if (pvi == s.moduleParams.end())
+if (pvi == s.moduleParams.end() || pvi->value.empty())
     {
     readBak = false;
     }
@@ -236,7 +241,7 @@ else
 
 pv.param = "WorkDir";
 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
-if (pvi == s.moduleParams.end())
+if (pvi == s.moduleParams.end() || pvi->value.empty())
     {
     errorStr = "Parameter \'WorkDir\' not found.";
     printfd(__FILE__, "Parameter 'WorkDir' not found\n");
@@ -249,8 +254,33 @@ if (workDir.size() && workDir[workDir.size() - 1] == '/')
     workDir.resize(workDir.size() - 1);
     }
 usersDir = workDir + "/users/";
+if (!CheckAndCreate(usersDir, GetConfModeDir()))
+    {
+    errorStr = usersDir + " doesn't exist. Failed to create.";
+    printfd(__FILE__, "%s\n", errorStr.c_str());
+    return -1;
+    }
 tariffsDir = workDir + "/tariffs/";
+if (!CheckAndCreate(tariffsDir, GetConfModeDir()))
+    {
+    errorStr = tariffsDir + " doesn't exist. Failed to create.";
+    printfd(__FILE__, "%s\n", errorStr.c_str());
+    return -1;
+    }
 adminsDir = workDir + "/admins/";
+if (!CheckAndCreate(adminsDir, GetConfModeDir()))
+    {
+    errorStr = adminsDir + " doesn't exist. Failed to create.";
+    printfd(__FILE__, "%s\n", errorStr.c_str());
+    return -1;
+    }
+servicesDir = workDir + "/services/";
+if (!CheckAndCreate(servicesDir, GetConfModeDir()))
+    {
+    errorStr = servicesDir + " doesn't exist. Failed to create.";
+    printfd(__FILE__, "%s\n", errorStr.c_str());
+    return -1;
+    }
 
 return 0;
 }
@@ -358,7 +388,7 @@ int FILES_STORE::ParseSettings()
 int ret = storeSettings.ParseSettings(settings);
 if (ret)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = storeSettings.GetStrError();
     }
 return ret;
@@ -370,12 +400,12 @@ std::vector<std::string> files;
 
 if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, ""))
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + std::string(strerror(errno));
     return -1;
     }
 
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 userList->swap(files);
 
@@ -388,12 +418,12 @@ std::vector<std::string> files;
 
 if (GetFileList(&files, storeSettings.GetAdminsDir(), S_IFREG, ".adm"))
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + std::string(strerror(errno));
     return -1;
     }
 
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 adminList->swap(files);
 
@@ -406,15 +436,33 @@ std::vector<std::string> files;
 
 if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf"))
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + std::string(strerror(errno));
     return -1;
     }
 
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 tariffList->swap(files);
 
+return 0;
+}
+//-----------------------------------------------------------------------------
+int FILES_STORE::GetServicesList(std::vector<std::string> * list) const
+{
+std::vector<std::string> files;
+
+if (GetFileList(&files, storeSettings.GetServicesDir(), S_IFREG, ".serv"))
+    {
+    STG_LOCKER lock(&mutex);
+    errorStr = "Failed to open '" + storeSettings.GetServicesDir() + "': " + std::string(strerror(errno));
+    return -1;
+    }
+
+STG_LOCKER lock(&mutex);
+
+list->swap(files);
+
 return 0;
 }
 //-----------------------------------------------------------------------------
@@ -448,7 +496,7 @@ while ((entry = readdir(d)))
         {
         if (unlink(str.c_str()))
             {
-            STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+            STG_LOCKER lock(&mutex);
             errorStr = "unlink failed. Message: '";
             errorStr += strerror(errno);
             errorStr += "'";
@@ -473,7 +521,7 @@ closedir(d);
 
 if (rmdir(path))
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "rmdir failed. Message: '";
     errorStr += strerror(errno);
     errorStr += "'";
@@ -492,7 +540,7 @@ strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str())
 
 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = std::string("mkdir failed. Message: '") + strerror(errno) + "'";
     printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
     return -1;
@@ -501,7 +549,7 @@ if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) ==
 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
 if (Touch(fileName))
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot create file \"" + fileName + "\'";
     printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
     return -1;
@@ -510,7 +558,7 @@ if (Touch(fileName))
 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
 if (Touch(fileName))
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot create file \"" + fileName + "\'";
     printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
     return -1;
@@ -528,7 +576,7 @@ if (access(dirName.c_str(), F_OK) != 0)
     {
     if (mkdir(dirName.c_str(), 0700) != 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Directory '" + dirName + "' cannot be created.";
         printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
         return -1;
@@ -541,7 +589,7 @@ if (access(dirName.c_str(), F_OK) == 0)
     strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
     if (rename(dirName1.c_str(), dirName.c_str()))
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
         printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
         return -1;
@@ -577,11 +625,10 @@ int FILES_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login, co
 {
 CONFIGFILE cf(fileName);
 int e = cf.Error();
-std::string str;
 
 if (e)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "User \'" + login + "\' data not read.";
     printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
     return -1;
@@ -589,14 +636,14 @@ if (e)
 
 if (cf.ReadString("Password", &conf->password, "") < 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "User \'" + login + "\' data not read. Parameter Password.";
     printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
     return -1;
     }
 if (conf->password.empty())
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "User \'" + login + "\' password is blank.";
     printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
     return -1;
@@ -604,14 +651,14 @@ if (conf->password.empty())
 
 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
     printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
     return -1;
     }
 if (conf->tariffName.empty())
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "User \'" + login + "\' tariff is blank.";
     printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
     return -1;
@@ -626,7 +673,7 @@ try
     }
 catch (const std::string & s)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
     printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
     return -1;
@@ -635,7 +682,7 @@ conf->ips = ips;
 
 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
     printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
     return -1;
@@ -643,7 +690,7 @@ if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
 
 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "User \'" + login + "\' data not read. Parameter Down.";
     printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
     return -1;
@@ -651,7 +698,7 @@ if (cf.ReadInt("down", &conf->disabled, 0) != 0)
 
 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
     printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
     return -1;
@@ -676,7 +723,7 @@ for (int i = 0; i < USERDATA_NUM; i++)
 
 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
     printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
     return -1;
@@ -709,7 +756,7 @@ int e = cf.Error();
 
 if (e)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
     printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
     return -1;
@@ -723,27 +770,27 @@ for (int i = 0; i < DIR_NUM; i++)
     snprintf(s, 22, "D%d", i);
     if (cf.ReadULongLongInt(s, &traff, 0) != 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
         printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
         return -1;
         }
-    stat->down[i] = traff;
+    stat->monthDown[i] = traff;
 
     snprintf(s, 22, "U%d", i);
     if (cf.ReadULongLongInt(s, &traff, 0) != 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr =   "User \'" + login + "\' stat not read. Parameter " + std::string(s);
         printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
         return -1;
         }
-    stat->up[i] = traff;
+    stat->monthUp[i] = traff;
     }
 
 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr =   "User \'" + login + "\' stat not read. Parameter Cash";
     printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
     return -1;
@@ -751,7 +798,7 @@ if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
 
 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr =   "User \'" + login + "\' stat not read. Parameter FreeMb";
     printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
     return -1;
@@ -759,7 +806,7 @@ if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
 
 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr =   "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
     printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
     return -1;
@@ -767,7 +814,7 @@ if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
 
 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr =   "User \'" + login + "\' stat not read. Parameter PassiveTime";
     printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
     return -1;
@@ -775,7 +822,7 @@ if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
 
 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr =   "User \'" + login + "\' stat not read. Parameter LastCashAdd";
     printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
     return -1;
@@ -783,7 +830,7 @@ if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
 
 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr =   "User \'" + login + "\' stat not read. Parameter LastActivityTime";
     printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
     return -1;
@@ -803,7 +850,7 @@ int e = cfstat.Error();
 
 if (e)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = std::string("User \'") + login + "\' conf not written\n";
     printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
     return -1;
@@ -814,7 +861,7 @@ e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGI
 
 if (e)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
     }
 
@@ -850,7 +897,6 @@ return 0;
 //-----------------------------------------------------------------------------
 int FILES_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
 {
-char s[22];
 std::string fileName;
 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
 
@@ -860,7 +906,7 @@ fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
 
     if (e)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = std::string("User \'") + login + "\' stat not written\n";
         printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
         return -1;
@@ -868,10 +914,11 @@ fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
 
     for (int i = 0; i < DIR_NUM; i++)
         {
+        char s[22];
         snprintf(s, 22, "D%d", i);
-        cfstat.WriteInt(s, stat.down[i]);
+        cfstat.WriteInt(s, stat.monthDown[i]);
         snprintf(s, 22, "U%d", i);
-        cfstat.WriteInt(s, stat.up[i]);
+        cfstat.WriteInt(s, stat.monthUp[i]);
         }
 
     cfstat.WriteDouble("Cash", stat.cash);
@@ -887,7 +934,7 @@ e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGI
 
 if (e)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
     }
 
@@ -912,7 +959,7 @@ if (f)
     }
 else
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot open \'" + fileName + "\'";
     printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
     return -1;
@@ -923,7 +970,7 @@ e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID(
 
 if (e)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
     }
 
@@ -948,7 +995,7 @@ if (f)
     }
 else
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot open \'" + fileName + "\'";
     printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
     return -1;
@@ -959,7 +1006,7 @@ e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID(
 
 if (e)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
     }
 
@@ -990,8 +1037,8 @@ return WriteLog2String(logStr, login);
 }
 //-----------------------------------------------------------------------------
 int FILES_STORE::WriteUserDisconnect(const std::string & login,
-                                     const DIR_TRAFF & up,
-                                     const DIR_TRAFF & down,
+                                     const DIR_TRAFF & monthUp,
+                                     const DIR_TRAFF & monthDown,
                                      const DIR_TRAFF & sessionUp,
                                      const DIR_TRAFF & sessionDown,
                                      double cash,
@@ -1005,9 +1052,9 @@ logStr << "Disconnect, "
        << "\' session download: \'"
        << sessionDown
        << "\' month upload: \'"
-       << up
+       << monthUp
        << "\' month download: \'"
-       << down
+       << monthDown
        << "\' cash: \'"
        << cash
        << "\'";
@@ -1036,7 +1083,7 @@ CONFIGFILE s(stat1, true);
 
 if (s.Error())
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot create file '" + stat1 + "'";
     printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
     return -1;
@@ -1051,7 +1098,7 @@ CONFIGFILE s2(stat2, true);
 
 if (s2.Error())
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot create file '" + stat2 + "'";
     printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
     return -1;
@@ -1061,11 +1108,11 @@ for (size_t i = 0; i < DIR_NUM; i++)
     {
     char dirName[3];
     snprintf(dirName, 3, "U%llu", (unsigned long long)i);
-    s.WriteInt(dirName, stat.up[i]); // Classic
-    s2.WriteInt(dirName, stat.up[i]); // New
+    s.WriteInt(dirName, stat.monthUp[i]); // Classic
+    s2.WriteInt(dirName, stat.monthUp[i]); // New
     snprintf(dirName, 3, "D%llu", (unsigned long long)i);
-    s.WriteInt(dirName, stat.down[i]); // Classic
-    s2.WriteInt(dirName, stat.down[i]); // New
+    s.WriteInt(dirName, stat.monthDown[i]); // Classic
+    s2.WriteInt(dirName, stat.monthDown[i]); // New
     }
 
 // Classic
@@ -1089,7 +1136,7 @@ strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_
 
 if (Touch(fileName))
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot create file " + fileName;
     printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
     return -1;
@@ -1104,7 +1151,7 @@ std::string fileName;
 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
 if (unlink(fileName.c_str()))
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "unlink failed. Message: '";
     errorStr += strerror(errno);
     errorStr += "'";
@@ -1115,10 +1162,6 @@ return 0;
 //-----------------------------------------------------------------------------*/
 int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
 {
-char passwordE[2 * ADM_PASSWD_LEN + 2];
-char pass[ADM_PASSWD_LEN + 1];
-char adminPass[ADM_PASSWD_LEN + 1];
-
 std::string fileName;
 
 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
@@ -1130,27 +1173,31 @@ strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login
 
     if (e)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Cannot write admin " + ac.login + ". " + fileName;
         printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
         return -1;
         }
 
+    char pass[ADM_PASSWD_LEN + 1];
     memset(pass, 0, sizeof(pass));
+
+    char adminPass[ADM_PASSWD_LEN + 1];
     memset(adminPass, 0, sizeof(adminPass));
 
     BLOWFISH_CTX ctx;
-    EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
+    InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
 
     strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
     adminPass[ADM_PASSWD_LEN - 1] = 0;
 
     for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
         {
-        EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
+        EncryptBlock(pass + 8*i, adminPass + 8*i, &ctx);
         }
 
     pass[ADM_PASSWD_LEN - 1] = 0;
+    char passwordE[2 * ADM_PASSWD_LEN + 2];
     Encode12(passwordE, pass, ADM_PASSWD_LEN);
 
     cf.WriteString("password", passwordE);
@@ -1182,7 +1229,7 @@ std::string p;
 
 if (cf.Error())
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot open " + fileName;
     printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
     return -1;
@@ -1190,7 +1237,7 @@ if (cf.Error())
 
 if (cf.ReadString("password", &p, "*"))
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Error in parameter password";
     printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
     return -1;
@@ -1204,11 +1251,11 @@ memset(pass, 0, sizeof(pass));
 if (passwordE[0] != 0)
     {
     Decode21(pass, passwordE);
-    EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
+    InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
 
     for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
         {
-        DecodeString(password + 8*i, pass + 8*i, &ctx);
+        DecryptBlock(password + 8*i, pass + 8*i, &ctx);
         }
     }
 else
@@ -1224,7 +1271,7 @@ if (cf.ReadUShortInt("ChgConf", &a, 0) == 0)
     ac->priv.userConf = a;
 else
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Error in parameter ChgConf";
     printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
     return -1;
@@ -1234,7 +1281,7 @@ if (cf.ReadUShortInt("ChgPassword", &a, 0) == 0)
     ac->priv.userPasswd = a;
 else
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Error in parameter ChgPassword";
     printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
     return -1;
@@ -1244,7 +1291,7 @@ if (cf.ReadUShortInt("ChgStat", &a, 0) == 0)
     ac->priv.userStat = a;
 else
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Error in parameter ChgStat";
     printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
     return -1;
@@ -1254,7 +1301,7 @@ if (cf.ReadUShortInt("ChgCash", &a, 0) == 0)
     ac->priv.userCash = a;
 else
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Error in parameter ChgCash";
     printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
     return -1;
@@ -1264,7 +1311,7 @@ if (cf.ReadUShortInt("UsrAddDel", &a, 0) == 0)
     ac->priv.userAddDel = a;
 else
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Error in parameter UsrAddDel";
     printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
     return -1;
@@ -1274,7 +1321,7 @@ if (cf.ReadUShortInt("ChgAdmin", &a, 0) == 0)
     ac->priv.adminChg = a;
 else
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Error in parameter ChgAdmin";
     printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
     return -1;
@@ -1284,7 +1331,7 @@ if (cf.ReadUShortInt("ChgTariff", &a, 0) == 0)
     ac->priv.tariffChg = a;
 else
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Error in parameter ChgTariff";
     printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
     return -1;
@@ -1309,7 +1356,7 @@ std::string fileName;
 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
 if (Touch(fileName))
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot create file " + fileName;
     printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
     return -1;
@@ -1323,7 +1370,7 @@ std::string fileName;
 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
 if (unlink(fileName.c_str()))
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "unlink failed. Message: '";
     errorStr += strerror(errno);
     errorStr += "'";
@@ -1341,7 +1388,7 @@ td->tariffConf.name = tariffName;
 
 if (conf.Error() != 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot read file " + fileName;
     printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
     return -1;
@@ -1353,7 +1400,7 @@ for (int i = 0; i<DIR_NUM; i++)
     strprintf(&param, "Time%d", i);
     if (conf.ReadString(param, &str, "00:00-00:00") < 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
         printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
         return -1;
@@ -1368,7 +1415,7 @@ for (int i = 0; i<DIR_NUM; i++)
     strprintf(&param, "PriceDayA%d", i);
     if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
         printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
         return -1;
@@ -1378,7 +1425,7 @@ for (int i = 0; i<DIR_NUM; i++)
     strprintf(&param, "PriceDayB%d", i);
     if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
         printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
         return -1;
@@ -1388,7 +1435,7 @@ for (int i = 0; i<DIR_NUM; i++)
     strprintf(&param, "PriceNightA%d", i);
     if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
         printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
         return -1;
@@ -1398,7 +1445,7 @@ for (int i = 0; i<DIR_NUM; i++)
     strprintf(&param, "PriceNightB%d", i);
     if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
         printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
         return -1;
@@ -1408,7 +1455,7 @@ for (int i = 0; i<DIR_NUM; i++)
     strprintf(&param, "Threshold%d", i);
     if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
         printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
         return -1;
@@ -1417,7 +1464,7 @@ for (int i = 0; i<DIR_NUM; i++)
     strprintf(&param, "SinglePrice%d", i);
     if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
         printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
         return -1;
@@ -1426,7 +1473,7 @@ for (int i = 0; i<DIR_NUM; i++)
     strprintf(&param, "NoDiscount%d", i);
     if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
         printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
         return -1;
@@ -1435,7 +1482,7 @@ for (int i = 0; i<DIR_NUM; i++)
 
 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
     printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
     return -1;
@@ -1443,7 +1490,7 @@ if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
 
 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
     printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
     return -1;
@@ -1451,7 +1498,7 @@ if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
 
 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
     printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
     return -1;
@@ -1459,30 +1506,25 @@ if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
 
 if (conf.ReadString("TraffType", &str, "") < 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
     printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
     return -1;
     }
 
-if (!strcasecmp(str.c_str(), "up"))
-    td->tariffConf.traffType = TRAFF_UP;
+td->tariffConf.traffType = TARIFF::StringToTraffType(str);
+
+if (conf.ReadString("Period", &str, "month") < 0)
+    td->tariffConf.period = TARIFF::MONTH;
 else
-    if (!strcasecmp(str.c_str(), "down"))
-        td->tariffConf.traffType = TRAFF_DOWN;
-    else
-        if (!strcasecmp(str.c_str(), "up+down"))
-            td->tariffConf.traffType = TRAFF_UP_DOWN;
-        else
-            if (!strcasecmp(str.c_str(), "max"))
-                td->tariffConf.traffType = TRAFF_MAX;
-            else
-                {
-                STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-                errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
-                printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str());
-                return -1;
-                }
+    td->tariffConf.period = TARIFF::StringToPeriod(str);
+
+if (conf.ReadString("ChangePolicy", &str, "allow") < 0)
+    td->tariffConf.changePolicy = TARIFF::ALLOW;
+else
+    td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(str);
+
+conf.ReadTime("ChangePolicyTimeout", &td->tariffConf.changePolicyTimeout, 0);
 return 0;
 }
 //-----------------------------------------------------------------------------
@@ -1497,7 +1539,7 @@ std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
 
     if (e)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Error writing tariff " + tariffName;
         printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
         return e;
@@ -1542,23 +1584,121 @@ std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
     cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
     cf.WriteDouble("Fee", td.tariffConf.fee);
     cf.WriteDouble("Free", td.tariffConf.free);
+    cf.WriteString("TraffType", TARIFF::TraffTypeToString(td.tariffConf.traffType));
+    cf.WriteString("Period", TARIFF::PeriodToString(td.tariffConf.period));
+    cf.WriteString("ChangePolicy", TARIFF::ChangePolicyToString(td.tariffConf.changePolicy));
+    cf.WriteTime("ChangePolicyTimeout", td.tariffConf.changePolicyTimeout);
+    }
+
+return 0;
+}
+//-----------------------------------------------------------------------------*/
+int FILES_STORE::AddService(const std::string & name) const
+{
+std::string fileName;
+strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str());
+
+if (Touch(fileName))
+    {
+    STG_LOCKER lock(&mutex);
+    errorStr = "Cannot create file " + fileName;
+    printfd(__FILE__, "FILES_STORE::AddService - failed to add service '%s'\n", name.c_str());
+    return -1;
+    }
+
+return 0;
+}
+//-----------------------------------------------------------------------------*/
+int FILES_STORE::DelService(const std::string & name) const
+{
+std::string fileName;
+strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str());
+if (unlink(fileName.c_str()))
+    {
+    STG_LOCKER lock(&mutex);
+    errorStr = "unlink failed. Message: '";
+    errorStr += strerror(errno);
+    errorStr += "'";
+    printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
+    }
+return 0;
+}
+//-----------------------------------------------------------------------------*/
+int FILES_STORE::SaveService(const SERVICE_CONF & conf) const
+{
+std::string fileName;
+
+strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), conf.name.c_str());
+
+    {
+    CONFIGFILE cf(fileName, true);
+
+    int e = cf.Error();
 
-    switch (td.tariffConf.traffType)
+    if (e)
         {
-        case TRAFF_UP:
-            cf.WriteString("TraffType", "up");
-            break;
-        case TRAFF_DOWN:
-            cf.WriteString("TraffType", "down");
-            break;
-        case TRAFF_UP_DOWN:
-            cf.WriteString("TraffType", "up+down");
-            break;
-        case TRAFF_MAX:
-            cf.WriteString("TraffType", "max");
-            break;
+        STG_LOCKER lock(&mutex);
+        errorStr = "Cannot write service " + conf.name + ". " + fileName;
+        printfd(__FILE__, "FILES_STORE::SaveService - failed to save service '%s'\n", conf.name.c_str());
+        return -1;
         }
+
+    cf.WriteString("name", conf.name);
+    cf.WriteString("comment", conf.comment);
+    cf.WriteDouble("cost", conf.cost);
+    cf.WriteInt("pay_day", conf.payDay);
+    }
+
+return 0;
+}
+//-----------------------------------------------------------------------------
+int FILES_STORE::RestoreService(SERVICE_CONF * conf, const std::string & name) const
+{
+std::string fileName;
+strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str());
+CONFIGFILE cf(fileName);
+
+if (cf.Error())
+    {
+    STG_LOCKER lock(&mutex);
+    errorStr = "Cannot open " + fileName;
+    printfd(__FILE__, "FILES_STORE::RestoreService - failed to restore service '%s'\n", name.c_str());
+    return -1;
+    }
+
+if (cf.ReadString("name", &conf->name, name))
+    {
+    STG_LOCKER lock(&mutex);
+    errorStr = "Error in parameter 'name'";
+    printfd(__FILE__, "FILES_STORE::RestoreService - name read failed for service '%s'\n", name.c_str());
+    return -1;
+    }
+
+if (cf.ReadString("comment", &conf->comment, ""))
+    {
+    STG_LOCKER lock(&mutex);
+    errorStr = "Error in parameter 'comment'";
+    printfd(__FILE__, "FILES_STORE::RestoreService - comment read failed for service '%s'\n", name.c_str());
+    return -1;
+    }
+
+if (cf.ReadDouble("cost", &conf->cost, 0.0))
+    {
+    STG_LOCKER lock(&mutex);
+    errorStr = "Error in parameter 'cost'";
+    printfd(__FILE__, "FILES_STORE::RestoreService - cost read failed for service '%s'\n", name.c_str());
+    return -1;
+    }
+
+unsigned short value = 0;
+if (cf.ReadUShortInt("pay_day", &value, 0))
+    {
+    STG_LOCKER lock(&mutex);
+    errorStr = "Error in parameter 'pay_day'";
+    printfd(__FILE__, "FILES_STORE::RestoreService - pay day read failed for service '%s'\n", name.c_str());
+    return -1;
     }
+conf->payDay = value;
 
 return 0;
 }
@@ -1580,7 +1720,7 @@ if (access(dn, F_OK) != 0)
     {
     if (mkdir(dn, 0700) != 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
         printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
         return -1;
@@ -1592,7 +1732,7 @@ e += chmod(dn, storeSettings.GetStatModeDir());
 
 if (e)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
     }
 
@@ -1613,7 +1753,7 @@ if (access(dn, F_OK) != 0)
     {
     if (mkdir(dn, 0700) != 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
         printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
         return -1;
@@ -1625,7 +1765,7 @@ e += chmod(dn, storeSettings.GetStatModeDir());
 
 if (e)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
     }
 
@@ -1639,7 +1779,7 @@ if (access(dn, F_OK) != 0)
     {
     if (mkdir(dn, 0700) != 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
         printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
         return -1;
@@ -1651,7 +1791,7 @@ e += chmod(dn, storeSettings.GetStatModeDir());
 
 if (e)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
     }
 
@@ -1661,7 +1801,7 @@ statFile = fopen (fn, "at");
 
 if (!statFile)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "File \'" + std::string(fn) + "\' cannot be written.";
     printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
     return -1;
@@ -1688,7 +1828,7 @@ s2 = lt2->tm_sec;
 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
             h1, m1, s1, h2, m2, s2) < 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = std::string("fprint failed. Message: '") + strerror(errno) + "'";
     printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
     fclose(statFile);
@@ -1712,7 +1852,7 @@ while (stIter != statTree.end())
                 u.c_str(),
                 stIter->second.cash) < 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "fprint failed. Message: '";
         errorStr += strerror(errno);
         errorStr += "'";
@@ -1728,7 +1868,7 @@ while (stIter != statTree.end())
                 u.c_str(),
                 stIter->second.cash) < 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = std::string("fprint failed. Message: '");
         errorStr += strerror(errno);
         errorStr += "'";
@@ -1748,7 +1888,7 @@ e += chmod(fn, storeSettings.GetStatMode());
 
 if (e)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
     }
 
@@ -1766,7 +1906,7 @@ if (access(dn.c_str(), F_OK) != 0)
     {
     if (mkdir(dn.c_str(), 0700) != 0)
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Directory \'";
         errorStr += dn;
         errorStr += "\' cannot be created.";
@@ -1784,7 +1924,7 @@ strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
 
 if (Touch(fn))
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "File \'";
     errorStr += fn;
     errorStr += "\' cannot be writen.";
@@ -1806,7 +1946,7 @@ if (access(fileName.c_str(), F_OK) != 0)
     {
     std::string idstr;
     x2str(msg.header.id, idstr);
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Message for user \'";
     errorStr += login + "\' with ID \'";
     errorStr += idstr + "\' does not exist.";
@@ -1819,14 +1959,14 @@ Touch(fileName + ".new");
 msgFile = fopen((fileName + ".new").c_str(), "wt");
 if (!msgFile)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "File \'" + fileName + "\' cannot be writen.";
     printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
     return -1;
     }
 
 bool res = true;
-res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0);
+res &= (fprintf(msgFile, "%u\n", msg.header.type) >= 0);
 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
@@ -1836,7 +1976,7 @@ res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
 
 if (!res)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = std::string("fprintf failed. Message: '") + strerror(errno) + "'";
     printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
     fclose(msgFile);
@@ -1849,9 +1989,9 @@ chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
 
 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
-    printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno));
+    printfd(__FILE__, "FILES_STORE::EditMessage - rename failed. Message: '%s'\n", strerror(errno));
     return -1;
     }
 
@@ -1894,7 +2034,7 @@ for (unsigned i = 0; i < messages.size(); i++)
         {
         if (unlink((dn + messages[i]).c_str()))
             {
-            STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+            STG_LOCKER lock(&mutex);
             errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'";
             printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
             return -1;
@@ -1912,7 +2052,7 @@ for (unsigned i = 0; i < messages.size(); i++)
         {
         if (unlink((dn + messages[i]).c_str()))
             {
-            STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+            STG_LOCKER lock(&mutex);
             errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'";
             printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
             return -1;
@@ -1934,7 +2074,7 @@ FILE * msgFile;
 msgFile = fopen(fileName.c_str(), "rt");
 if (!msgFile)
     {
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    STG_LOCKER lock(&mutex);
     errorStr = "File \'";
     errorStr += fileName;
     errorStr += "\' cannot be openned.";
@@ -1955,7 +2095,7 @@ memset(p, 0, sizeof(p));
 for (int pos = 0; pos < 6; pos++)
     {
     if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Cannot read file \'";
         errorStr += fileName;
         errorStr += "\'. Missing data.";
@@ -1973,7 +2113,7 @@ for (int pos = 0; pos < 6; pos++)
 
     if (feof(msgFile))
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Cannot read file \'";
         errorStr += fileName;
         errorStr += "\'. Missing data.";
@@ -1985,7 +2125,7 @@ for (int pos = 0; pos < 6; pos++)
 
     if (str2x(p, *(d[pos])))
         {
-        STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+        STG_LOCKER lock(&mutex);
         errorStr = "Cannot read file \'";
         errorStr += fileName;
         errorStr += "\'. Incorrect value. \'";