]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store_admins.cpp
87d633440b0bb89be14d8d7e1a449efb71cd0c7e
[stg.git] / projects / stargazer / plugins / store / firebird / firebird_store_admins.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 /*
22  *  Administrators manipulation methods
23  *
24  *  $Revision: 1.11 $
25  *  $Date: 2008/12/04 17:10:06 $
26  *
27  */
28
29 #include "firebird_store.h"
30
31 #include "stg/admin_conf.h"
32 #include "stg/blowfish.h"
33 #include "stg/common.h"
34
35 #include <string>
36 #include <vector>
37
38 #define adm_enc_passwd "cjeifY8m3"
39
40 //-----------------------------------------------------------------------------
41 int FIREBIRD_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
42 {
43 STG_LOCKER lock(&mutex);
44
45 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
46 IBPP::Statement st = IBPP::StatementFactory(db, tr);
47
48 try
49     {
50     tr->Start();
51     st->Execute("select login from tb_admins");
52     while (st->Fetch())
53         {
54         std::string login;
55         st->Get(1, login);
56         adminsList->push_back(login);
57         }
58     tr->Commit();
59     }
60
61 catch (IBPP::Exception & ex)
62     {
63     tr->Rollback();
64     strError = "IBPP exception";
65     printfd(__FILE__, ex.what());
66     return -1;
67     }
68
69 return 0;
70 }
71 //-----------------------------------------------------------------------------
72 int FIREBIRD_STORE::SaveAdmin(const STG::AdminConf & ac) const
73 {
74 STG_LOCKER lock(&mutex);
75
76 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
77 IBPP::Statement st = IBPP::StatementFactory(db, tr);
78
79 char encodedPass[2 * ADM_PASSWD_LEN + 2];
80 char cryptedPass[ADM_PASSWD_LEN + 1];
81 char adminPass[ADM_PASSWD_LEN + 1];
82 BLOWFISH_CTX ctx;
83
84 memset(cryptedPass, 0, ADM_PASSWD_LEN + 1);
85 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
86 InitContext(adm_enc_passwd, sizeof(adm_enc_passwd), &ctx);
87
88 for (int i = 0; i < ADM_PASSWD_LEN / 8; i++)
89     EncryptBlock(cryptedPass + 8 * i, adminPass + 8 * i, &ctx);
90
91 cryptedPass[ADM_PASSWD_LEN] = 0;
92 Encode12(encodedPass, cryptedPass, ADM_PASSWD_LEN);
93
94 try
95     {
96     tr->Start();
97     st->Prepare("update tb_admins set passwd=?, \
98                chg_conf=?, \
99                chg_password=?, \
100                chg_stat=?, \
101                chg_cash=?, \
102                usr_add_del=?, \
103                chg_tariff=?, \
104                chg_admin=? \
105                where login=?");
106     st->Set(1, encodedPass);
107     st->Set(2, static_cast<int16_t>(ac.priv.userConf));
108     st->Set(3, static_cast<int16_t>(ac.priv.userPasswd));
109     st->Set(4, static_cast<int16_t>(ac.priv.userStat));
110     st->Set(5, static_cast<int16_t>(ac.priv.userCash));
111     st->Set(6, static_cast<int16_t>(ac.priv.userAddDel));
112     st->Set(7, static_cast<int16_t>(ac.priv.tariffChg));
113     st->Set(8, static_cast<int16_t>(ac.priv.adminChg));
114     st->Set(9, ac.login);
115     st->Execute();
116     tr->Commit();
117     }
118
119 catch (IBPP::Exception & ex)
120     {
121     tr->Rollback();
122     strError = "IBPP exception";
123     printfd(__FILE__, ex.what());
124     return -1;
125     }
126
127 return 0;
128 }
129 //-----------------------------------------------------------------------------
130 int FIREBIRD_STORE::RestoreAdmin(STG::AdminConf * ac, const std::string & login) const
131 {
132 STG_LOCKER lock(&mutex);
133
134 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
135 IBPP::Statement st = IBPP::StatementFactory(db, tr);
136
137 char cryptedPass[ADM_PASSWD_LEN + 1];
138 char adminPass[ADM_PASSWD_LEN + 1];
139 BLOWFISH_CTX ctx;
140
141 try
142     {
143     tr->Start();
144     st->Prepare("select * from tb_admins where login = ?");
145     st->Set(1, login);
146     st->Execute();
147     if (st->Fetch())
148         {
149         st->Get(2, ac->login);
150         st->Get(3, ac->password);
151         st->Get(4, reinterpret_cast<int16_t &>(ac->priv.userConf));
152         st->Get(5, reinterpret_cast<int16_t &>(ac->priv.userPasswd));
153         st->Get(6, reinterpret_cast<int16_t &>(ac->priv.userStat));
154         st->Get(7, reinterpret_cast<int16_t &>(ac->priv.userCash));
155         st->Get(8, reinterpret_cast<int16_t &>(ac->priv.userAddDel));
156         st->Get(9, reinterpret_cast<int16_t &>(ac->priv.tariffChg));
157         st->Get(10, reinterpret_cast<int16_t &>(ac->priv.adminChg));
158         }
159     else
160         {
161         strError = "Admin \"" + login + "\" not found in database";
162     printfd(__FILE__, "Admin '%s' not found in database\n", login.c_str());
163     tr->Rollback();
164         return -1;
165         }
166     tr->Commit();
167     }
168
169 catch (IBPP::Exception & ex)
170     {
171     tr->Rollback();
172     strError = "IBPP exception";
173     printfd(__FILE__, ex.what());
174     return -1;
175     }
176
177 if (ac->password == "")
178     {
179     return 0;
180     }
181
182 Decode21(cryptedPass, ac->password.c_str());
183 InitContext(adm_enc_passwd, sizeof(adm_enc_passwd), &ctx);
184 for (int i = 0; i < ADM_PASSWD_LEN / 8; i++)
185     {
186     DecryptBlock(adminPass + 8 * i, cryptedPass + 8 * i, &ctx);
187     }
188 ac->password = adminPass;
189
190 return 0;
191 }
192 //-----------------------------------------------------------------------------
193 int FIREBIRD_STORE::AddAdmin(const std::string & login) const
194 {
195 STG_LOCKER lock(&mutex);
196
197 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
198 IBPP::Statement st = IBPP::StatementFactory(db, tr);
199
200 try
201     {
202     tr->Start();
203     st->Prepare("insert into tb_admins(login, \
204                     passwd, \
205                     chg_conf, \
206                     chg_password, \
207                     chg_stat, \
208                     chg_cash, \
209                     usr_add_del, \
210                     chg_tariff, \
211                     chg_admin, \
212                     chg_service, \
213                     chg_corporation) \
214                  values (?, '', 0, 0, 0, 0, 0, 0, 0, 0, 0)");
215     st->Set(1, login);
216     st->Execute();
217     tr->Commit();
218     }
219
220 catch (IBPP::Exception & ex)
221     {
222     tr->Rollback();
223     strError = "IBPP exception";
224     printfd(__FILE__, ex.what());
225     return -1;
226     }
227
228 return 0;
229 }
230 //-----------------------------------------------------------------------------
231 int FIREBIRD_STORE::DelAdmin(const std::string & login) const
232 {
233 STG_LOCKER lock(&mutex);
234
235 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
236 IBPP::Statement st = IBPP::StatementFactory(db, tr);
237
238 try
239     {
240     tr->Start();
241     st->Prepare("delete from tb_admins where login = ?");
242     st->Set(1, login);
243     st->Execute();
244     tr->Commit();
245     }
246
247 catch (IBPP::Exception & ex)
248     {
249     strError = "IBPP exception";
250     printfd(__FILE__, ex.what());
251     return -1;
252     }
253
254 return 0;
255 }
256 //-----------------------------------------------------------------------------
257