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