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