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