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