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/locker.h"
32 #include "stg/common.h"
33 #include "stg/admin_conf.h"
34 #include "stg/blowfish.h"
42 #define adm_enc_passwd "cjeifY8m3"
44 //-----------------------------------------------------------------------------
45 int POSTGRESQL_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
47 STG_LOCKER lock(&mutex);
49 if (PQstatus(connection) != CONNECTION_OK)
51 printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
54 strError = "Connection lost";
55 printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): '%s'\n", strError.c_str());
62 if (StartTransaction())
64 printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): 'Failed to start transaction'\n");
68 result = PQexec(connection, "SELECT login FROM tb_admins WHERE login <> '@stargazer'");
70 if (PQresultStatus(result) != PGRES_TUPLES_OK)
72 strError = PQresultErrorMessage(result);
74 printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): '%s'\n", strError.c_str());
75 if (RollbackTransaction())
77 printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): 'Failed to rollback transaction'\n");
82 int tuples = PQntuples(result);
84 for (int i = 0; i < tuples; ++i)
86 adminsList->push_back(PQgetvalue(result, i, 0));
91 if (CommitTransaction())
93 printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): 'Failed to commit transaction'\n");
100 //-----------------------------------------------------------------------------
101 int POSTGRESQL_STORE::SaveAdmin(const STG::AdminConf & ac) const
103 STG_LOCKER lock(&mutex);
105 if (PQstatus(connection) != CONNECTION_OK)
107 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
110 strError = "Connection lost";
111 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): '%s'\n", strError.c_str());
118 if (StartTransaction())
120 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to start transaction'\n");
124 char encodedPass[2 * ADM_PASSWD_LEN + 2];
125 char cryptedPass[ADM_PASSWD_LEN + 1];
126 char adminPass[ADM_PASSWD_LEN + 1];
129 memset(cryptedPass, 0, ADM_PASSWD_LEN + 1);
130 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
131 InitContext(adm_enc_passwd, sizeof(adm_enc_passwd), &ctx);
133 for (int i = 0; i < ADM_PASSWD_LEN / 8; i++)
134 EncryptBlock(cryptedPass + 8 * i, adminPass + 8 * i, &ctx);
136 cryptedPass[ADM_PASSWD_LEN] = 0;
137 Encode12(encodedPass, cryptedPass, ADM_PASSWD_LEN);
139 std::string pass = encodedPass;
140 std::string login = ac.login;
142 if (EscapeString(pass))
144 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to escape password'\n");
145 if (RollbackTransaction())
147 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to rollback transaction'\n");
152 if (EscapeString(login))
154 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to escape login'\n");
155 if (RollbackTransaction())
157 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to rollback transaction'\n");
162 std::stringstream query;
163 query << "UPDATE tb_admins SET "
164 << "passwd = '" << pass << "', "
165 << "chg_conf = " << ac.priv.userConf << ", "
166 << "chg_password = " << ac.priv.userPasswd << ", "
167 << "chg_stat = " << ac.priv.userStat << ", "
168 << "chg_cash = " << ac.priv.userCash << ", "
169 << "usr_add_del = " << ac.priv.userAddDel << ", "
170 << "chg_tariff = " << ac.priv.tariffChg << ", "
171 << "chg_admin = " << ac.priv.adminChg << " "
172 << "WHERE login = '" << login << "'";
174 result = PQexec(connection, query.str().c_str());
176 if (PQresultStatus(result) != PGRES_COMMAND_OK)
178 strError = PQresultErrorMessage(result);
180 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): '%s'\n", strError.c_str());
181 if (RollbackTransaction())
183 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to rollback transaction'\n");
190 if (CommitTransaction())
192 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to commit transaction'\n");
199 //-----------------------------------------------------------------------------
200 int POSTGRESQL_STORE::RestoreAdmin(STG::AdminConf * ac, const std::string & login) const
202 STG_LOCKER lock(&mutex);
204 if (PQstatus(connection) != CONNECTION_OK)
206 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
209 strError = "Connection lost";
210 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): '%s'\n", strError.c_str());
217 if (StartTransaction())
219 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to start transaction'\n");
223 char cryptedPass[ADM_PASSWD_LEN + 1];
224 char adminPass[ADM_PASSWD_LEN + 1];
227 std::string elogin = login;
229 if (EscapeString(elogin))
231 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to escape login'\n");
232 if (RollbackTransaction())
234 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to rollback transaction'\n");
239 std::ostringstream query;
240 query << "SELECT login, passwd, \
241 chg_conf, chg_password, chg_stat, \
242 chg_cash, usr_add_del, chg_tariff, \
243 chg_admin, chg_service, chg_corporation \
245 WHERE login = '" << elogin << "'";
247 result = PQexec(connection, query.str().c_str());
249 if (PQresultStatus(result) != PGRES_TUPLES_OK)
251 strError = PQresultErrorMessage(result);
252 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): '%s'\n", strError.c_str());
254 if (RollbackTransaction())
256 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to rollback transaction'\n");
261 int tuples = PQntuples(result);
265 strError = "Failed to fetch admin's data";
266 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
268 if (RollbackTransaction())
270 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to rollback transaction'\n");
275 ac->login = PQgetvalue(result, 0, 0);
276 ac->password = PQgetvalue(result, 0, 1);
278 std::stringstream tuple;
279 tuple << PQgetvalue(result, 0, 2) << " "
280 << PQgetvalue(result, 0, 3) << " "
281 << PQgetvalue(result, 0, 4) << " "
282 << PQgetvalue(result, 0, 5) << " "
283 << PQgetvalue(result, 0, 6) << " "
284 << PQgetvalue(result, 0, 7) << " "
285 << PQgetvalue(result, 0, 8) << " "
286 << PQgetvalue(result, 0, 9) << " "
287 << PQgetvalue(result, 0, 10);
291 tuple >> ac->priv.userConf
292 >> ac->priv.userPasswd
295 >> ac->priv.userAddDel
296 >> ac->priv.tariffChg
297 >> ac->priv.adminChg;
299 if (CommitTransaction())
301 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to commit transacion'\n");
305 if (ac->password == "")
310 Decode21(cryptedPass, ac->password.c_str());
311 InitContext(adm_enc_passwd, sizeof(adm_enc_passwd), &ctx);
312 for (int i = 0; i < ADM_PASSWD_LEN / 8; i++)
314 DecryptBlock(adminPass + 8 * i, cryptedPass + 8 * i, &ctx);
316 ac->password = adminPass;
320 //-----------------------------------------------------------------------------
321 int POSTGRESQL_STORE::AddAdmin(const std::string & login) const
323 STG_LOCKER lock(&mutex);
325 if (PQstatus(connection) != CONNECTION_OK)
327 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
330 strError = "Connection lost";
331 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): '%s'\n", strError.c_str());
338 if (StartTransaction())
340 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to start transaction'\n");
344 std::string elogin = login;
346 if (EscapeString(elogin))
348 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to escape login'\n");
349 if (RollbackTransaction())
351 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to rollback transaction'\n");
356 std::ostringstream query;
357 query << "INSERT INTO tb_admins \
359 chg_conf, chg_password, chg_stat, \
360 chg_cash, usr_add_del, chg_tariff, \
361 chg_admin, chg_service, chg_corporation) \
363 << "('" << elogin << "', \
364 '', 0, 0, 0, 0, 0, 0, 0, 0, 0)";
366 result = PQexec(connection, query.str().c_str());
368 if (PQresultStatus(result) != PGRES_COMMAND_OK)
370 strError = PQresultErrorMessage(result);
372 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): '%s'\n", strError.c_str());
373 if (RollbackTransaction())
375 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to rollback transaction'\n");
382 if (CommitTransaction())
384 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to commit transaction'\n");
390 //-----------------------------------------------------------------------------
391 int POSTGRESQL_STORE::DelAdmin(const std::string & login) const
393 STG_LOCKER lock(&mutex);
395 if (PQstatus(connection) != CONNECTION_OK)
397 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
400 strError = "Connection lost";
401 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): '%s'\n", strError.c_str());
408 if (StartTransaction())
410 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to start transaction'\n");
414 std::string elogin = login;
416 if (EscapeString(elogin))
418 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to escape login'\n");
419 if (RollbackTransaction())
421 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to rollback transaction'\n");
426 std::ostringstream query;
427 query << "DELETE FROM tb_admins WHERE login = '" << elogin << "'";
429 result = PQexec(connection, query.str().c_str());
431 if (PQresultStatus(result) != PGRES_COMMAND_OK)
433 strError = PQresultErrorMessage(result);
435 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): '%s'\n", strError.c_str());
436 if (RollbackTransaction())
438 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to rollback transaction'\n");
445 if (CommitTransaction())
447 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to commit transaction'\n");
453 //-----------------------------------------------------------------------------