]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp
Add std namespace to the string in rpcconfig.cpp
[stg.git] / projects / stargazer / plugins / configuration / rpcconfig / rpcconfig.cpp
1 #include <cstdlib>
2 #include <csignal>
3
4 #include "rpcconfig.h"
5
6 #include "admin.h"
7 #include "module_settings.h"
8
9 #include "info_methods.h"
10 #include "users_methods.h"
11 #include "tariffs_methods.h"
12 #include "admins_methods.h"
13 #include "messages_methods.h"
14
15 class RPC_CONFIG_CREATOR {
16 private:
17     RPC_CONFIG * rpcconfig;
18
19 public:
20     RPC_CONFIG_CREATOR()
21         : rpcconfig(new RPC_CONFIG())
22         {
23         }
24     ~RPC_CONFIG_CREATOR()
25         {
26         delete rpcconfig;
27         }
28
29     RPC_CONFIG * GetPlugin()
30         {
31         return rpcconfig;
32         }
33 };
34
35 RPC_CONFIG_CREATOR rpcc;
36
37 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
38     : errorStr(),
39       port(0),
40       cookieTimeout(0)
41 {
42 }
43
44 int RPC_CONFIG_SETTINGS::ParseIntInRange(const std::string & str,
45                                          int min,
46                                          int max,
47                                          int * val)
48 {
49 if (str2x(str.c_str(), *val))
50     {
51     errorStr = "Incorrect value \'" + str + "\'.";
52     return -1;
53     }
54 if (*val < min || *val > max)
55     {
56     errorStr = "Value \'" + str + "\' out of range.";
57     return -1;
58     }
59 return 0;
60 }
61
62 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
63 {
64 int p;
65 PARAM_VALUE pv;
66 vector<PARAM_VALUE>::const_iterator pvi;
67
68 pv.param = "Port";
69 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
70 if (pvi == s.moduleParams.end())
71     {
72     errorStr = "Parameter \'Port\' not found.";
73     printfd(__FILE__, "Parameter 'Port' not found\n");
74     return -1;
75     }
76 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
77     {
78     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
79     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
80     return -1;
81     }
82 port = p;
83
84 pv.param = "CookieTimeout";
85 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
86 if (pvi == s.moduleParams.end())
87     {
88     cookieTimeout = 1800; // 30 * 60
89     }
90 else
91     {
92     if (str2x(pvi->value[0], cookieTimeout))
93         {
94         errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
95         printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
96         return -1;
97         }
98     }
99
100 return 0;
101 }
102
103 BASE_PLUGIN * GetPlugin()
104 {
105 return rpcc.GetPlugin();
106 }
107
108 RPC_CONFIG::RPC_CONFIG()
109     : users(NULL),
110       admins(NULL),
111       tariffs(NULL),
112       store(NULL),
113       stgSettings(NULL),
114       rpcServer(NULL),
115       running(false),
116       stopped(true)
117 {
118 }
119
120 RPC_CONFIG::~RPC_CONFIG()
121 {
122 // delete server
123 delete rpcServer;
124 }
125
126 int RPC_CONFIG::ParseSettings()
127 {
128 int ret = rpcConfigSettings.ParseSettings(settings);
129
130 if (ret)
131     errorStr = rpcConfigSettings.GetStrError();
132
133 return ret;
134 }
135
136 int RPC_CONFIG::Start()
137 {
138 InitiateRegistry();
139 running = true;
140 rpcServer = new xmlrpc_c::serverAbyss(
141         rpcRegistry,
142         rpcConfigSettings.GetPort(),
143         "/var/log/stargazer_rpc.log"
144         );
145 if (pthread_create(&tid, NULL, Run, this))
146     {
147     errorStr = "Failed to create RPC thread";
148     printfd(__FILE__, "Failed to crate RPC thread\n");
149     return -1;
150     }
151 return 0;
152 }
153
154 int RPC_CONFIG::Stop()
155 {
156 running = false;
157 for (int i = 0; i < 5 && !stopped; ++i)
158     usleep(200000);
159 //rpcServer->terminate();
160 if (!stopped)
161     {
162     if (pthread_kill(tid, SIGTERM))
163         {
164         errorStr = "Failed to kill thread";
165         printfd(__FILE__, "Failed to kill thread\n");
166         }
167     for (int i = 0; i < 25 && !stopped; ++i)
168         usleep(200000);
169     if (!stopped)
170         {
171         printfd(__FILE__, "Failed to stop RPC thread\n");
172         errorStr = "Failed to stop RPC thread";
173         return -1;
174         }
175     else
176         {
177         pthread_join(tid, NULL);
178         }
179     }
180 return 0;
181 }
182
183 void * RPC_CONFIG::Run(void * rc)
184 {
185 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
186
187 config->stopped = false;
188 while (config->running)
189     {
190     config->rpcServer->runOnce();
191     }
192 config->stopped = true;
193
194 return NULL;
195 }
196
197 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
198                               ADMIN_INFO * info)
199 {
200 std::map<std::string,
201          ADMIN_INFO>::iterator it;
202
203 it = cookies.find(cookie);
204
205 if (it == cookies.end())
206     {
207     return true;
208     }
209
210 if (difftime(it->second.accessTime, time(NULL)) >
211     rpcConfigSettings.GetCookieTimeout())
212     {
213     cookies.erase(it);
214     return true;
215     }
216
217 // Update access time
218 time(&it->second.accessTime);
219 *info = it->second;
220 return false;
221 }
222
223 bool RPC_CONFIG::CheckAdmin(const std::string & login,
224                             const std::string & password,
225                             std::string * cookie)
226 {
227 ADMIN admin;
228
229 if (!admins->AdminCorrect(login, password, &admin))
230     {
231     return true;
232     }
233
234 ADMIN_INFO info;
235 time(&info.accessTime);
236 info.admin = login;
237 info.priviledges = *admin.GetPriv();
238 *cookie = GetCookie();
239 cookies[*cookie] = info;
240
241 return false;
242 }
243
244 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
245 {
246 std::map<std::string,
247          ADMIN_INFO>::iterator it;
248
249 it = cookies.find(cookie);
250
251 if (it == cookies.end())
252     {
253     return true;
254     }
255
256 cookies.erase(it);
257
258 return false;
259 }
260
261 std::string RPC_CONFIG::GetCookie() const
262 {
263 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
264 std::string cookie;
265
266 for (int i = 0; i < 64; ++i)
267     {
268     cookie += charset[rand() % charset.length()];
269     };
270
271 return cookie;
272 }
273
274 void RPC_CONFIG::InitiateRegistry()
275 {
276 // manage registry
277 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
278             tariffs,
279             users,
280             stgSettings
281             ));
282 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
283
284 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
285             this
286             ));
287 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
288
289 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
290             this
291             ));
292 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
293
294 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
295             this,
296             users
297             ));
298 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
299
300 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
301             this,
302             admins,
303             users
304             ));
305 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
306
307 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
308             this,
309             admins,
310             users
311             ));
312 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
313
314 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
315             this,
316             users
317             ));
318 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
319
320 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
321             this,
322             admins,
323             tariffs,
324             store,
325             users
326             ));
327 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
328
329 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
330             this,
331             admins,
332             store,
333             users
334             ));
335 rpcRegistry.addMethod("stargazer.add_cash", methodAddCashPtr);
336
337 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
338             this,
339             admins,
340             store,
341             users
342             ));
343 rpcRegistry.addMethod("stargazer.set_cash", methodSetCashPtr);
344
345 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
346             this,
347             admins,
348             tariffs,
349             store,
350             users
351             ));
352 rpcRegistry.addMethod("stargazer.tariff_change", methodTariffChangePtr);
353
354 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
355             this,
356             tariffs
357             ));
358 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
359
360 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
361             this,
362             admins,
363             tariffs
364             ));
365 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
366
367 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
368             this,
369             tariffs
370             ));
371 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
372
373 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
374             this,
375             admins,
376             tariffs
377             ));
378 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
379
380 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
381             this,
382             admins,
383             tariffs,
384             users
385             ));
386 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
387
388 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
389             this,
390             admins
391             ));
392 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
393
394 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
395             this,
396             admins
397             ));
398 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
399
400 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
401             this,
402             admins
403             ));
404 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
405
406 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
407             this,
408             admins
409             ));
410 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
411
412 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
413             this,
414             admins
415             ));
416 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
417
418 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
419             this,
420             users
421             ));
422 rpcRegistry.addMethod("stargazer.send_message", methodSendMessagePtr);
423
424 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
425             this,
426             users
427             ));
428 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);
429 }
430