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