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