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