]> git.stg.codes - stg.git/blob - stargazer/admins_impl.cpp
Public interfaces: part 1
[stg.git] / 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 "admins_impl.h"
32 #include "admin_impl.h"
33
34 #include "stg/common.h"
35
36 #include <algorithm>
37 #include <cerrno>
38 #include <cassert>
39
40 using STG::AdminsImpl;
41
42 //-----------------------------------------------------------------------------
43 AdminsImpl::AdminsImpl(Store * st)
44     : stg(Priv(0xFFFF), "@stargazer", ""),
45       noAdmin(Priv(0xFFFF), "NO-ADMIN", ""),
46       data(),
47       store(st),
48       WriteServLog(Logger::get()),
49       searchDescriptors(),
50       handle(0),
51       mutex(),
52       strError()
53 {
54 pthread_mutex_init(&mutex, NULL);
55 Read();
56 }
57 //-----------------------------------------------------------------------------
58 int AdminsImpl::Add(const std::string & login, const Admin * admin)
59 {
60 STG_LOCKER lock(&mutex);
61 const Priv * priv = admin->GetPriv();
62
63 if (!priv->adminChg)
64     {
65     std::string s = admin->GetLogStr() + " Add administrator \'" + login + "\'. Access denied.";
66     strError = "Access denied.";
67     WriteServLog(s.c_str());
68     return -1;
69     }
70
71 AdminImpl adm(Priv(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 AdminsImpl::Del(const std::string & login, const Admin * admin)
98 {
99 STG_LOCKER lock(&mutex);
100 const Priv * priv = admin->GetPriv();
101
102 if (!priv->adminChg)
103     {
104     std::string s = admin->GetLogStr() + " Delete administrator \'" + login + "\'. Access denied.";
105     strError = "Access denied.";
106     WriteServLog(s.c_str());
107     return -1;
108     }
109
110 admin_iter ai(find(data.begin(), data.end(), AdminImpl(Priv(0), login, "")));
111
112 if (ai == data.end())
113     {
114     strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist.";
115     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
116     return -1;
117     }
118
119 std::map<int, const_admin_iter>::iterator si;
120 si = searchDescriptors.begin();
121 while (si != searchDescriptors.end())
122     {
123     if (si->second == ai)
124         (si->second)++;
125     ++si;
126     }
127
128 data.erase(ai);
129 if (store->DelAdmin(login) < 0)
130     {
131     strError = "Administrator \'" + login + "\' was not deleted. Error: " + store->GetStrError();
132     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
133
134     return -1;
135     }
136
137 WriteServLog("%s Administrator \'%s\' deleted.", admin->GetLogStr().c_str(), login.c_str());
138 return 0;
139 }
140 //-----------------------------------------------------------------------------
141 int AdminsImpl::Change(const AdminConf & ac, const Admin * admin)
142 {
143 STG_LOCKER lock(&mutex);
144 const Priv * priv = admin->GetPriv();
145
146 if (!priv->adminChg)
147     {
148     std::string s = admin->GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied.";
149     strError = "Access denied.";
150     WriteServLog(s.c_str());
151     return -1;
152     }
153
154 admin_iter ai(find(data.begin(), data.end(), AdminImpl(Priv(0), ac.login, "")));
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 AdminsImpl::Read()
178 {
179 STG_LOCKER lock(&mutex);
180 std::vector<std::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     AdminConf ac(Priv(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(AdminImpl(ac));
198     }
199 return 0;
200 }
201 //-----------------------------------------------------------------------------
202 bool AdminsImpl::Find(const std::string & l, Admin ** admin)
203 {
204 assert(admin != NULL && "Pointer to admin is not null");
205
206 STG_LOCKER lock(&mutex);
207 if (data.empty())
208     {
209     printfd(__FILE__, "No admin in system!\n");
210     *admin = &noAdmin;
211     return false;
212     }
213
214 admin_iter ai(find(data.begin(), data.end(), AdminImpl(Priv(0), l, "")));
215
216 if (ai != data.end())
217     {
218     *admin = &(*ai);
219     return false;
220     }
221
222 return true;
223 }
224 //-----------------------------------------------------------------------------
225 bool AdminsImpl::Exists(const std::string & login) const
226 {
227 STG_LOCKER lock(&mutex);
228 if (data.empty())
229     {
230     printfd(__FILE__, "no admin in system!\n");
231     return true;
232     }
233
234 const_admin_iter ai(find(data.begin(), data.end(), AdminImpl(Priv(0), login, "")));
235
236 if (ai != data.end())
237     return true;
238
239 return false;
240 }
241 //-----------------------------------------------------------------------------
242 bool AdminsImpl::Correct(const std::string & login, const std::string & password, Admin ** admin)
243 {
244 STG_LOCKER lock(&mutex);
245 if (data.empty())
246     {
247     printfd(__FILE__, "no admin in system!\n");
248     return true;
249     }
250
251 admin_iter ai(find(data.begin(), data.end(), AdminImpl(Priv(0), login, "")));
252
253 if (ai == data.end())
254     {
255     return false;
256     }
257
258 if (ai->GetPassword() != password)
259     {
260     return false;
261     }
262
263 *admin = &(*ai);
264
265 return true;
266 }
267 //-----------------------------------------------------------------------------
268 int AdminsImpl::OpenSearch() const
269 {
270 STG_LOCKER lock(&mutex);
271 handle++;
272 searchDescriptors[handle] = data.begin();
273 return handle;
274 }
275 //-----------------------------------------------------------------------------
276 int AdminsImpl::SearchNext(int h, AdminConf * ac) const
277 {
278 STG_LOCKER lock(&mutex);
279 if (searchDescriptors.find(h) == searchDescriptors.end())
280     {
281     WriteServLog("ADMINS. Incorrect search handle.");
282     return -1;
283     }
284
285 if (searchDescriptors[h] == data.end())
286     return -1;
287
288 AdminImpl a = *searchDescriptors[h]++;
289
290 *ac = a.GetConf();
291
292 return 0;
293 }
294 //-----------------------------------------------------------------------------
295 int AdminsImpl::CloseSearch(int h) const
296 {
297 STG_LOCKER lock(&mutex);
298 if (searchDescriptors.find(h) != searchDescriptors.end())
299     {
300     searchDescriptors.erase(searchDescriptors.find(h));
301     return 0;
302     }
303
304 WriteServLog("ADMINS. Incorrect search handle.");
305 return -1;
306 }
307 //-----------------------------------------------------------------------------