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