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 : Maxim Mamontov <faust@stargazer.dp.ua>
 
  22  *  Administrators manipulation methods
 
  25  *  $Date: 2010/11/08 10:10:24 $
 
  29 #include "postgresql_store.h"
 
  31 #include "stg/common.h"
 
  32 #include "stg/admin_conf.h"
 
  33 #include "stg/blowfish.h"
 
  41 #define adm_enc_passwd "cjeifY8m3"
 
  43 //-----------------------------------------------------------------------------
 
  44 int POSTGRESQL_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
 
  46 std::lock_guard lock(m_mutex);
 
  48 if (PQstatus(connection) != CONNECTION_OK)
 
  50     printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
 
  53         strError = "Connection lost";
 
  54         printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): '%s'\n", strError.c_str());
 
  61 if (StartTransaction())
 
  63     printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): 'Failed to start transaction'\n");
 
  67 result = PQexec(connection, "SELECT login FROM tb_admins WHERE login <> '@stargazer'");
 
  69 if (PQresultStatus(result) != PGRES_TUPLES_OK)
 
  71     strError = PQresultErrorMessage(result);
 
  73     printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): '%s'\n", strError.c_str());
 
  74     if (RollbackTransaction())
 
  76         printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): 'Failed to rollback transaction'\n");
 
  81 int tuples = PQntuples(result);
 
  83 for (int i = 0; i < tuples; ++i)
 
  85     adminsList->push_back(PQgetvalue(result, i, 0));
 
  90 if (CommitTransaction())
 
  92     printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): 'Failed to commit transaction'\n");
 
  99 //-----------------------------------------------------------------------------
 
 100 int POSTGRESQL_STORE::SaveAdmin(const STG::AdminConf & ac) const
 
 102 std::lock_guard lock(m_mutex);
 
 104 if (PQstatus(connection) != CONNECTION_OK)
 
 106     printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
 
 109         strError = "Connection lost";
 
 110         printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): '%s'\n", strError.c_str());
 
 117 if (StartTransaction())
 
 119     printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to start transaction'\n");
 
 123 char encodedPass[2 * ADM_PASSWD_LEN + 2];
 
 124 char cryptedPass[ADM_PASSWD_LEN + 1];
 
 125 char adminPass[ADM_PASSWD_LEN + 1];
 
 128 memset(cryptedPass, 0, ADM_PASSWD_LEN + 1);
 
 129 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
 
 130 InitContext(adm_enc_passwd, sizeof(adm_enc_passwd), &ctx);
 
 132 for (int i = 0; i < ADM_PASSWD_LEN / 8; i++)
 
 133     EncryptBlock(cryptedPass + 8 * i, adminPass + 8 * i, &ctx);
 
 135 cryptedPass[ADM_PASSWD_LEN] = 0;
 
 136 Encode12(encodedPass, cryptedPass, ADM_PASSWD_LEN);
 
 138 std::string pass = encodedPass;
 
 139 std::string login = ac.login;
 
 141 if (EscapeString(pass))
 
 143     printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to escape password'\n");
 
 144     if (RollbackTransaction())
 
 146         printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to rollback transaction'\n");
 
 151 if (EscapeString(login))
 
 153     printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to escape login'\n");
 
 154     if (RollbackTransaction())
 
 156         printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to rollback transaction'\n");
 
 161 std::stringstream query;
 
 162 query << "UPDATE tb_admins SET "
 
 163           << "passwd = '" << pass << "', "
 
 164           << "chg_conf = " << ac.priv.userConf << ", "
 
 165           << "chg_password = " << ac.priv.userPasswd << ", "
 
 166           << "chg_stat = " << ac.priv.userStat << ", "
 
 167           << "chg_cash = " << ac.priv.userCash << ", "
 
 168           << "usr_add_del = " << ac.priv.userAddDel << ", "
 
 169           << "chg_tariff = " << ac.priv.tariffChg << ", "
 
 170           << "chg_admin = " << ac.priv.adminChg << " "
 
 171       << "WHERE login = '" << login << "'";
 
 173 result = PQexec(connection, query.str().c_str());
 
 175 if (PQresultStatus(result) != PGRES_COMMAND_OK)
 
 177     strError = PQresultErrorMessage(result);
 
 179     printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): '%s'\n", strError.c_str());
 
 180     if (RollbackTransaction())
 
 182         printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to rollback transaction'\n");
 
 189 if (CommitTransaction())
 
 191     printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to commit transaction'\n");
 
 198 //-----------------------------------------------------------------------------
 
 199 int POSTGRESQL_STORE::RestoreAdmin(STG::AdminConf * ac, const std::string & login) const
 
 201 std::lock_guard lock(m_mutex);
 
 203 if (PQstatus(connection) != CONNECTION_OK)
 
 205     printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
 
 208         strError = "Connection lost";
 
 209         printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): '%s'\n", strError.c_str());
 
 216 if (StartTransaction())
 
 218     printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to start transaction'\n");
 
 222 char cryptedPass[ADM_PASSWD_LEN + 1];
 
 223 char adminPass[ADM_PASSWD_LEN + 1];
 
 226 std::string elogin = login;
 
 228 if (EscapeString(elogin))
 
 230     printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to escape login'\n");
 
 231     if (RollbackTransaction())
 
 233         printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to rollback transaction'\n");
 
 238 std::ostringstream query;
 
 239 query << "SELECT login, passwd, \
 
 240                  chg_conf, chg_password, chg_stat, \
 
 241                  chg_cash, usr_add_del, chg_tariff, \
 
 242                  chg_admin, chg_service, chg_corporation \
 
 244           WHERE login = '" << elogin << "'";
 
 246 result = PQexec(connection, query.str().c_str());
 
 248 if (PQresultStatus(result) != PGRES_TUPLES_OK)
 
 250     strError = PQresultErrorMessage(result);
 
 251     printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): '%s'\n", strError.c_str());
 
 253     if (RollbackTransaction())
 
 255         printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to rollback transaction'\n");
 
 260 int tuples = PQntuples(result);
 
 264     strError = "Failed to fetch admin's data";
 
 265     printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
 
 267     if (RollbackTransaction())
 
 269         printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to rollback transaction'\n");
 
 274 ac->login = PQgetvalue(result, 0, 0);
 
 275 ac->password = PQgetvalue(result, 0, 1);
 
 277 std::stringstream tuple;
 
 278 tuple << PQgetvalue(result, 0, 2) << " "
 
 279       << PQgetvalue(result, 0, 3) << " "
 
 280       << PQgetvalue(result, 0, 4) << " "
 
 281       << PQgetvalue(result, 0, 5) << " "
 
 282       << PQgetvalue(result, 0, 6) << " "
 
 283       << PQgetvalue(result, 0, 7) << " "
 
 284       << PQgetvalue(result, 0, 8) << " "
 
 285       << PQgetvalue(result, 0, 9) << " "
 
 286       << PQgetvalue(result, 0, 10);
 
 290 tuple >> ac->priv.userConf
 
 291       >> ac->priv.userPasswd
 
 294       >> ac->priv.userAddDel
 
 295       >> ac->priv.tariffChg
 
 296       >> ac->priv.adminChg;
 
 298 if (CommitTransaction())
 
 300     printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to commit transacion'\n");
 
 304 if (ac->password == "")
 
 309 Decode21(cryptedPass, ac->password.c_str());
 
 310 InitContext(adm_enc_passwd, sizeof(adm_enc_passwd), &ctx);
 
 311 for (int i = 0; i < ADM_PASSWD_LEN / 8; i++)
 
 313     DecryptBlock(adminPass + 8 * i, cryptedPass + 8 * i, &ctx);
 
 315 ac->password = adminPass;
 
 319 //-----------------------------------------------------------------------------
 
 320 int POSTGRESQL_STORE::AddAdmin(const std::string & login) const
 
 322 std::lock_guard lock(m_mutex);
 
 324 if (PQstatus(connection) != CONNECTION_OK)
 
 326     printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
 
 329         strError = "Connection lost";
 
 330         printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): '%s'\n", strError.c_str());
 
 337 if (StartTransaction())
 
 339     printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to start transaction'\n");
 
 343 std::string elogin = login;
 
 345 if (EscapeString(elogin))
 
 347     printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to escape login'\n");
 
 348     if (RollbackTransaction())
 
 350         printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to rollback transaction'\n");
 
 355 std::ostringstream query;
 
 356 query << "INSERT INTO tb_admins \
 
 358               chg_conf, chg_password, chg_stat, \
 
 359               chg_cash, usr_add_del, chg_tariff, \
 
 360               chg_admin, chg_service, chg_corporation) \
 
 362           << "('" << elogin << "', \
 
 363               '', 0, 0, 0, 0, 0, 0, 0, 0, 0)";
 
 365 result = PQexec(connection, query.str().c_str());
 
 367 if (PQresultStatus(result) != PGRES_COMMAND_OK)
 
 369     strError = PQresultErrorMessage(result);
 
 371     printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): '%s'\n", strError.c_str());
 
 372     if (RollbackTransaction())
 
 374         printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to rollback transaction'\n");
 
 381 if (CommitTransaction())
 
 383     printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to commit transaction'\n");
 
 389 //-----------------------------------------------------------------------------
 
 390 int POSTGRESQL_STORE::DelAdmin(const std::string & login) const
 
 392 std::lock_guard lock(m_mutex);
 
 394 if (PQstatus(connection) != CONNECTION_OK)
 
 396     printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
 
 399         strError = "Connection lost";
 
 400         printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): '%s'\n", strError.c_str());
 
 407 if (StartTransaction())
 
 409     printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to start transaction'\n");
 
 413 std::string elogin = login;
 
 415 if (EscapeString(elogin))
 
 417     printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to escape login'\n");
 
 418     if (RollbackTransaction())
 
 420         printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to rollback transaction'\n");
 
 425 std::ostringstream query;
 
 426 query << "DELETE FROM tb_admins WHERE login = '" << elogin << "'";
 
 428 result = PQexec(connection, query.str().c_str());
 
 430 if (PQresultStatus(result) != PGRES_COMMAND_OK)
 
 432     strError = PQresultErrorMessage(result);
 
 434     printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): '%s'\n", strError.c_str());
 
 435     if (RollbackTransaction())
 
 437         printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to rollback transaction'\n");
 
 444 if (CommitTransaction())
 
 446     printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to commit transaction'\n");
 
 452 //-----------------------------------------------------------------------------