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 $
35 #include "stg/stg_locker.h"
36 #include "stg/admin_conf.h"
37 #include "stg/blowfish.h"
38 #include "postgresql_store.h"
40 #define adm_enc_passwd "cjeifY8m3"
42 //-----------------------------------------------------------------------------
43 int POSTGRESQL_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
45 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
47 if (PQstatus(connection) != CONNECTION_OK)
49 printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
52 strError = "Connection lost";
53 printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): '%s'\n", strError.c_str());
60 if (StartTransaction())
62 printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): 'Failed to start transaction'\n");
66 result = PQexec(connection, "SELECT login FROM tb_admins WHERE login <> '@stargazer'");
68 if (PQresultStatus(result) != PGRES_TUPLES_OK)
70 strError = PQresultErrorMessage(result);
72 printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): '%s'\n", strError.c_str());
73 if (RollbackTransaction())
75 printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): 'Failed to rollback transaction'\n");
80 int tuples = PQntuples(result);
82 for (int i = 0; i < tuples; ++i)
84 adminsList->push_back(PQgetvalue(result, i, 0));
89 if (CommitTransaction())
91 printfd(__FILE__, "POSTGRESQL_STORE::GetAdminsList(): 'Failed to commit transaction'\n");
98 //-----------------------------------------------------------------------------
99 int POSTGRESQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
101 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
103 if (PQstatus(connection) != CONNECTION_OK)
105 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
108 strError = "Connection lost";
109 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): '%s'\n", strError.c_str());
116 if (StartTransaction())
118 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to start transaction'\n");
122 char encodedPass[2 * ADM_PASSWD_LEN + 2];
123 char cryptedPass[ADM_PASSWD_LEN + 1];
124 char adminPass[ADM_PASSWD_LEN + 1];
127 memset(cryptedPass, 0, ADM_PASSWD_LEN + 1);
128 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
129 EnDecodeInit(adm_enc_passwd, sizeof(adm_enc_passwd), &ctx);
131 for (int i = 0; i < ADM_PASSWD_LEN / 8; i++)
132 EncodeString(cryptedPass + 8 * i, adminPass + 8 * i, &ctx);
134 cryptedPass[ADM_PASSWD_LEN] = 0;
135 Encode12(encodedPass, cryptedPass, ADM_PASSWD_LEN);
137 std::string password = encodedPass;
138 std::string login = ac.login;
140 if (EscapeString(password))
142 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to escape password'\n");
143 if (RollbackTransaction())
145 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to rollback transaction'\n");
150 if (EscapeString(login))
152 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to escape login'\n");
153 if (RollbackTransaction())
155 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to rollback transaction'\n");
160 std::stringstream query;
161 query << "UPDATE tb_admins SET "
162 << "passwd = '" << password << "', "
163 << "chg_conf = " << ac.priv.userConf << ", "
164 << "chg_password = " << ac.priv.userPasswd << ", "
165 << "chg_stat = " << ac.priv.userStat << ", "
166 << "chg_cash = " << ac.priv.userCash << ", "
167 << "usr_add_del = " << ac.priv.userAddDel << ", "
168 << "chg_tariff = " << ac.priv.tariffChg << ", "
169 << "chg_admin = " << ac.priv.adminChg << " "
170 << "WHERE login = '" << login << "'";
172 result = PQexec(connection, query.str().c_str());
174 if (PQresultStatus(result) != PGRES_COMMAND_OK)
176 strError = PQresultErrorMessage(result);
178 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): '%s'\n", strError.c_str());
179 if (RollbackTransaction())
181 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to rollback transaction'\n");
188 if (CommitTransaction())
190 printfd(__FILE__, "POSTGRESQL_STORE::SaveAdmin(): 'Failed to commit transaction'\n");
197 //-----------------------------------------------------------------------------
198 int POSTGRESQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
200 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
202 if (PQstatus(connection) != CONNECTION_OK)
204 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
207 strError = "Connection lost";
208 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): '%s'\n", strError.c_str());
215 if (StartTransaction())
217 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to start transaction'\n");
221 char cryptedPass[ADM_PASSWD_LEN + 1];
222 char adminPass[ADM_PASSWD_LEN + 1];
225 std::string elogin = login;
227 if (EscapeString(elogin))
229 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to escape login'\n");
230 if (RollbackTransaction())
232 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to rollback transaction'\n");
237 std::stringstream query;
238 query << "SELECT login, passwd, \
239 chg_conf, chg_password, chg_stat, \
240 chg_cash, usr_add_del, chg_tariff, \
241 chg_admin, chg_service, chg_corporation \
243 WHERE login = '" << elogin << "'";
245 result = PQexec(connection, query.str().c_str());
247 if (PQresultStatus(result) != PGRES_TUPLES_OK)
249 strError = PQresultErrorMessage(result);
250 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): '%s'\n", strError.c_str());
252 if (RollbackTransaction())
254 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to rollback transaction'\n");
259 int tuples = PQntuples(result);
263 strError = "Failed to fetch admin's data";
264 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
266 if (RollbackTransaction())
268 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to rollback transaction'\n");
273 ac->login = PQgetvalue(result, 0, 0);
274 ac->password = PQgetvalue(result, 0, 1);
276 std::stringstream tuple;
277 tuple << PQgetvalue(result, 0, 2) << " "
278 << PQgetvalue(result, 0, 3) << " "
279 << PQgetvalue(result, 0, 4) << " "
280 << PQgetvalue(result, 0, 5) << " "
281 << PQgetvalue(result, 0, 6) << " "
282 << PQgetvalue(result, 0, 7) << " "
283 << PQgetvalue(result, 0, 8) << " "
284 << PQgetvalue(result, 0, 9) << " "
285 << PQgetvalue(result, 0, 10);
289 tuple >> ac->priv.userConf
290 >> ac->priv.userPasswd
293 >> ac->priv.userAddDel
294 >> ac->priv.tariffChg
295 >> ac->priv.adminChg;
297 if (CommitTransaction())
299 printfd(__FILE__, "POSTGRESQL_STORE::RestoreAdmin(): 'Failed to commit transacion'\n");
303 if (ac->password == "")
308 Decode21(cryptedPass, ac->password.c_str());
309 EnDecodeInit(adm_enc_passwd, sizeof(adm_enc_passwd), &ctx);
310 for (int i = 0; i < ADM_PASSWD_LEN / 8; i++)
312 DecodeString(adminPass + 8 * i, cryptedPass + 8 * i, &ctx);
314 ac->password = adminPass;
318 //-----------------------------------------------------------------------------
319 int POSTGRESQL_STORE::AddAdmin(const string & login) const
321 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
323 if (PQstatus(connection) != CONNECTION_OK)
325 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
328 strError = "Connection lost";
329 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): '%s'\n", strError.c_str());
336 if (StartTransaction())
338 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to start transaction'\n");
342 std::string elogin = login;
344 if (EscapeString(elogin))
346 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to escape login'\n");
347 if (RollbackTransaction())
349 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to rollback transaction'\n");
354 std::stringstream query;
355 query << "INSERT INTO tb_admins \
357 chg_conf, chg_password, chg_stat, \
358 chg_cash, usr_add_del, chg_tariff, \
359 chg_admin, chg_service, chg_corporation) \
361 << "('" << elogin << "', \
362 '', 0, 0, 0, 0, 0, 0, 0, 0, 0)";
364 result = PQexec(connection, query.str().c_str());
366 if (PQresultStatus(result) != PGRES_COMMAND_OK)
368 strError = PQresultErrorMessage(result);
370 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): '%s'\n", strError.c_str());
371 if (RollbackTransaction())
373 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to rollback transaction'\n");
380 if (CommitTransaction())
382 printfd(__FILE__, "POSTGRESQL_STORE::AddAdmin(): 'Failed to commit transaction'\n");
388 //-----------------------------------------------------------------------------
389 int POSTGRESQL_STORE::DelAdmin(const string & login) const
391 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
393 if (PQstatus(connection) != CONNECTION_OK)
395 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
398 strError = "Connection lost";
399 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): '%s'\n", strError.c_str());
406 if (StartTransaction())
408 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to start transaction'\n");
412 std::string elogin = login;
414 if (EscapeString(elogin))
416 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to escape login'\n");
417 if (RollbackTransaction())
419 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to rollback transaction'\n");
424 std::stringstream query;
425 query << "DELETE FROM tb_admins WHERE login = '" << elogin << "'";
427 result = PQexec(connection, query.str().c_str());
429 if (PQresultStatus(result) != PGRES_COMMAND_OK)
431 strError = PQresultErrorMessage(result);
433 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): '%s'\n", strError.c_str());
434 if (RollbackTransaction())
436 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to rollback transaction'\n");
443 if (CommitTransaction())
445 printfd(__FILE__, "POSTGRESQL_STORE::DelAdmin(): 'Failed to commit transaction'\n");
451 //-----------------------------------------------------------------------------