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: 2008/12/04 17:10:06 $
29 #include "firebird_store.h"
32 #include "stg/admin_conf.h"
33 #include "stg/blowfish.h"
34 #include "stg/common.h"
39 #define adm_enc_passwd "cjeifY8m3"
41 //-----------------------------------------------------------------------------
42 int FIREBIRD_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
44 STG_LOCKER lock(&mutex);
46 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
47 IBPP::Statement st = IBPP::StatementFactory(db, tr);
54 st->Execute("select login from tb_admins");
58 adminsList->push_back(login);
63 catch (IBPP::Exception & ex)
66 strError = "IBPP exception";
67 printfd(__FILE__, ex.what());
73 //-----------------------------------------------------------------------------
74 int FIREBIRD_STORE::SaveAdmin(const STG::AdminConf & ac) const
76 STG_LOCKER lock(&mutex);
78 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
79 IBPP::Statement st = IBPP::StatementFactory(db, tr);
81 char encodedPass[2 * ADM_PASSWD_LEN + 2];
82 char cryptedPass[ADM_PASSWD_LEN + 1];
83 char adminPass[ADM_PASSWD_LEN + 1];
86 memset(cryptedPass, 0, ADM_PASSWD_LEN + 1);
87 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
88 InitContext(adm_enc_passwd, sizeof(adm_enc_passwd), &ctx);
90 for (int i = 0; i < ADM_PASSWD_LEN / 8; i++)
91 EncryptBlock(cryptedPass + 8 * i, adminPass + 8 * i, &ctx);
93 cryptedPass[ADM_PASSWD_LEN] = 0;
94 Encode12(encodedPass, cryptedPass, ADM_PASSWD_LEN);
99 st->Prepare("update tb_admins set passwd=?, \
108 st->Set(1, encodedPass);
109 st->Set(2, static_cast<int16_t>(ac.priv.userConf));
110 st->Set(3, static_cast<int16_t>(ac.priv.userPasswd));
111 st->Set(4, static_cast<int16_t>(ac.priv.userStat));
112 st->Set(5, static_cast<int16_t>(ac.priv.userCash));
113 st->Set(6, static_cast<int16_t>(ac.priv.userAddDel));
114 st->Set(7, static_cast<int16_t>(ac.priv.tariffChg));
115 st->Set(8, static_cast<int16_t>(ac.priv.adminChg));
116 st->Set(9, ac.login);
121 catch (IBPP::Exception & ex)
124 strError = "IBPP exception";
125 printfd(__FILE__, ex.what());
131 //-----------------------------------------------------------------------------
132 int FIREBIRD_STORE::RestoreAdmin(STG::AdminConf * ac, const std::string & login) const
134 STG_LOCKER lock(&mutex);
136 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
137 IBPP::Statement st = IBPP::StatementFactory(db, tr);
139 char cryptedPass[ADM_PASSWD_LEN + 1];
140 char adminPass[ADM_PASSWD_LEN + 1];
146 st->Prepare("select * from tb_admins where login = ?");
151 st->Get(2, ac->login);
152 st->Get(3, ac->password);
153 st->Get(4, (int16_t &)ac->priv.userConf);
154 st->Get(5, (int16_t &)ac->priv.userPasswd);
155 st->Get(6, (int16_t &)ac->priv.userStat);
156 st->Get(7, (int16_t &)ac->priv.userCash);
157 st->Get(8, (int16_t &)ac->priv.userAddDel);
158 st->Get(9, (int16_t &)ac->priv.tariffChg);
159 st->Get(10, (int16_t &)ac->priv.adminChg);
163 strError = "Admin \"" + login + "\" not found in database";
164 printfd(__FILE__, "Admin '%s' not found in database\n", login.c_str());
171 catch (IBPP::Exception & ex)
174 strError = "IBPP exception";
175 printfd(__FILE__, ex.what());
179 if (ac->password == "")
184 Decode21(cryptedPass, ac->password.c_str());
185 InitContext(adm_enc_passwd, sizeof(adm_enc_passwd), &ctx);
186 for (int i = 0; i < ADM_PASSWD_LEN / 8; i++)
188 DecryptBlock(adminPass + 8 * i, cryptedPass + 8 * i, &ctx);
190 ac->password = adminPass;
194 //-----------------------------------------------------------------------------
195 int FIREBIRD_STORE::AddAdmin(const std::string & login) const
197 STG_LOCKER lock(&mutex);
199 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
200 IBPP::Statement st = IBPP::StatementFactory(db, tr);
205 st->Prepare("insert into tb_admins(login, \
216 values (?, '', 0, 0, 0, 0, 0, 0, 0, 0, 0)");
222 catch (IBPP::Exception & ex)
225 strError = "IBPP exception";
226 printfd(__FILE__, ex.what());
232 //-----------------------------------------------------------------------------
233 int FIREBIRD_STORE::DelAdmin(const std::string & login) const
235 STG_LOCKER lock(&mutex);
237 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
238 IBPP::Statement st = IBPP::StatementFactory(db, tr);
243 st->Prepare("delete from tb_admins where login = ?");
249 catch (IBPP::Exception & ex)
251 strError = "IBPP exception";
252 printfd(__FILE__, ex.what());
258 //-----------------------------------------------------------------------------