]> git.stg.codes - stg.git/blob - projects/stargazer/admins_impl.cpp
Simplified STG_LOCKER.
[stg.git] / projects / stargazer / admins_impl.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  *    Date: 27.10.2002
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25  /*
26  $Revision: 1.15 $
27  $Date: 2010/10/04 20:17:12 $
28  $Author: faust $
29  */
30
31 #include <cerrno>
32 #include <cassert>
33 #include <algorithm>
34
35 #include "stg/common.h"
36 #include "admins_impl.h"
37 #include "admin_impl.h"
38
39 using namespace std;
40
41 //-----------------------------------------------------------------------------
42 ADMINS_IMPL::ADMINS_IMPL(STORE * st)
43     : ADMINS(),
44       stg(0xFFFF, "@stargazer", ""),
45       noAdmin(0xFFFF, "NO-ADMIN", ""),
46       data(),
47       store(st),
48       WriteServLog(GetStgLogger()),
49       searchDescriptors(),
50       handle(0),
51       mutex(),
52       strError()
53 {
54 pthread_mutex_init(&mutex, NULL);
55 Read();
56 }
57 //-----------------------------------------------------------------------------
58 int ADMINS_IMPL::Add(const string & login, const ADMIN * admin)
59 {
60 STG_LOCKER lock(&mutex);
61 const PRIV * priv = admin->GetPriv();
62
63 if (!priv->adminChg)
64     {
65     string s = admin->GetLogStr() + " Add administrator \'" + login + "\'. Access denied.";
66     strError = "Access denied.";
67     WriteServLog(s.c_str());
68     return -1;
69     }
70
71 ADMIN_IMPL adm(0, login, "");
72 admin_iter ai(find(data.begin(), data.end(), adm));
73
74 if (ai != data.end())
75     {
76     strError = "Administrator \'" + login + "\' cannot not be added. Administrator already exist.";
77     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
78
79     return -1;
80     }
81
82 data.push_back(adm);
83
84 if (store->AddAdmin(login) == 0)
85     {
86     WriteServLog("%s Administrator \'%s\' added.",
87                  admin->GetLogStr().c_str(), login.c_str());
88     return 0;
89     }
90
91 strError = "Administrator \'" + login + "\' was not added. Error: " + store->GetStrError();
92 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
93
94 return -1;
95 }
96 //-----------------------------------------------------------------------------
97 int ADMINS_IMPL::Del(const string & login, const ADMIN * admin)
98 {
99 STG_LOCKER lock(&mutex);
100 ADMIN_IMPL adm(0, login, "");
101 const PRIV * priv = admin->GetPriv();
102
103 if (!priv->adminChg)
104     {
105     string s = admin->GetLogStr() + " Delete administrator \'" + login + "\'. Access denied.";
106     strError = "Access denied.";
107     WriteServLog(s.c_str());
108     return -1;
109     }
110
111 admin_iter ai(find(data.begin(), data.end(), adm));
112
113 if (ai == data.end())
114     {
115     strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist.";
116     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
117     return -1;
118     }
119
120 map<int, const_admin_iter>::iterator si;
121 si = searchDescriptors.begin();
122 while (si != searchDescriptors.end())
123     {
124     if (si->second == ai)
125         (si->second)++;
126     si++;
127     }
128
129 data.remove(*ai);
130 if (store->DelAdmin(login) < 0)
131     {
132     strError = "Administrator \'" + login + "\' was not deleted. Error: " + store->GetStrError();
133     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
134
135     return -1;
136     }
137
138 WriteServLog("%s Administrator \'%s\' deleted.", admin->GetLogStr().c_str(), login.c_str());
139 return 0;
140 }
141 //-----------------------------------------------------------------------------
142 int ADMINS_IMPL::Change(const ADMIN_CONF & ac, const ADMIN * admin)
143 {
144 STG_LOCKER lock(&mutex);
145 const PRIV * priv = admin->GetPriv();
146
147 if (!priv->adminChg)
148     {
149     string s = admin->GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied.";
150     strError = "Access denied.";
151     WriteServLog(s.c_str());
152     return -1;
153     }
154
155 ADMIN_IMPL adm(0, ac.login, "");
156 admin_iter ai(find(data.begin(), data.end(), adm));
157
158 if (ai == data.end())
159     {
160     strError = "Administrator \'" + ac.login + "\' cannot be changed " + ". Administrator does not exist.";
161     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
162     return -1;
163     }
164
165 *ai = ac;
166 if (store->SaveAdmin(ac))
167     {
168     WriteServLog("Cannot write admin %s.", ac.login.c_str());
169     WriteServLog("%s", store->GetStrError().c_str());
170     return -1;
171     }
172
173 WriteServLog("%s Administrator \'%s\' changed.",
174              admin->GetLogStr().c_str(), ac.login.c_str());
175
176 return 0;
177 }
178 //-----------------------------------------------------------------------------
179 int ADMINS_IMPL::Read()
180 {
181 STG_LOCKER lock(&mutex);
182 vector<string> adminsList;
183 if (store->GetAdminsList(&adminsList) < 0)
184     {
185     WriteServLog(store->GetStrError().c_str());
186     return -1;
187     }
188
189 for (unsigned int i = 0; i < adminsList.size(); i++)
190     {
191     ADMIN_CONF ac(0, adminsList[i], "");
192
193     if (store->RestoreAdmin(&ac, adminsList[i]))
194         {
195         WriteServLog(store->GetStrError().c_str());
196         return -1;
197         }
198
199     data.push_back(ADMIN_IMPL(ac));
200     }
201 return 0;
202 }
203 //-----------------------------------------------------------------------------
204 bool ADMINS_IMPL::Find(const string & l, ADMIN ** admin)
205 {
206 assert(admin != NULL && "Pointer to admin is not null");
207
208 STG_LOCKER lock(&mutex);
209 if (data.empty())
210     {
211     printfd(__FILE__, "No admin in system!\n");
212     *admin = &noAdmin;
213     return false;
214     }
215
216 ADMIN_IMPL adm(0, l, "");
217 admin_iter ai(find(data.begin(), data.end(), adm));
218
219 if (ai != data.end())
220     {
221     *admin = &(*ai);
222     return false;
223     }
224
225 return true;
226 }
227 //-----------------------------------------------------------------------------
228 bool ADMINS_IMPL::Exists(const string & login) const
229 {
230 STG_LOCKER lock(&mutex);
231 if (data.empty())
232     {
233     printfd(__FILE__, "no admin in system!\n");
234     return true;
235     }
236
237 ADMIN_IMPL adm(0, login, "");
238 const_admin_iter ai(find(data.begin(), data.end(), adm));
239
240 if (ai != data.end())
241     return true;
242
243 return false;
244 }
245 //-----------------------------------------------------------------------------
246 bool ADMINS_IMPL::Correct(const string & login, const std::string & password, ADMIN ** admin)
247 {
248 STG_LOCKER lock(&mutex);
249 if (data.empty())
250     {
251     printfd(__FILE__, "no admin in system!\n");
252     return true;
253     }
254
255 ADMIN_IMPL adm(0, login, "");
256 admin_iter ai(find(data.begin(), data.end(), adm));
257
258 if (ai == data.end())
259     {
260     return false;
261     }
262
263 if (ai->GetPassword() != password)
264     {
265     return false;
266     }
267
268 *admin = &(*ai);
269
270 return true;
271 }
272 //-----------------------------------------------------------------------------
273 int ADMINS_IMPL::OpenSearch() const
274 {
275 STG_LOCKER lock(&mutex);
276 handle++;
277 searchDescriptors[handle] = data.begin();
278 return handle;
279 }
280 //-----------------------------------------------------------------------------
281 int ADMINS_IMPL::SearchNext(int h, ADMIN_CONF * ac) const
282 {
283 STG_LOCKER lock(&mutex);
284 if (searchDescriptors.find(h) == searchDescriptors.end())
285     {
286     WriteServLog("ADMINS. Incorrect search handle.");
287     return -1;
288     }
289
290 if (searchDescriptors[h] == data.end())
291     return -1;
292
293 ADMIN_IMPL a = *searchDescriptors[h]++;
294
295 *ac = a.GetConf();
296
297 return 0;
298 }
299 //-----------------------------------------------------------------------------
300 int ADMINS_IMPL::CloseSearch(int h) const
301 {
302 STG_LOCKER lock(&mutex);
303 if (searchDescriptors.find(h) != searchDescriptors.end())
304     {
305     searchDescriptors.erase(searchDescriptors.find(h));
306     return 0;
307     }
308
309 WriteServLog("ADMINS. Incorrect search handle.");
310 return -1;
311 }
312 //-----------------------------------------------------------------------------