]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp
Add <ostream> to fix missing header in xmlrpc-c
[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 {
101 }
102
103 RPC_CONFIG::~RPC_CONFIG()
104 {
105 // delete server
106 delete rpcServer;
107 }
108
109 int RPC_CONFIG::ParseSettings()
110 {
111 int ret = rpcConfigSettings.ParseSettings(settings);
112
113 if (ret)
114     errorStr = rpcConfigSettings.GetStrError();
115
116 return ret;
117 }
118
119 void RPC_CONFIG::SetStgSettings(const SETTINGS * settings)
120 {
121     dayFee = settings->GetDayFee();
122     dirNames.erase(dirNames.begin(), dirNames.end());
123     for (size_t i = 0; i < DIR_NUM; ++i) {
124         dirNames.push_back(settings->GetDirName(i));
125     }
126 }
127
128 int RPC_CONFIG::Start()
129 {
130 InitiateRegistry();
131 running = true;
132
133 fd = socket(AF_INET, SOCK_STREAM, 0);
134 if (fd < 0)
135     {
136     errorStr = "Failed to create socket";
137     printfd(__FILE__, "Failed to create listening socket: %s\n", strerror(errno));
138     return -1;
139     }
140
141 int flag = 1;
142
143 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)))
144     {
145     errorStr = "Setsockopt failed.";
146     printfd(__FILE__, "Setsockopt failed: %s\n", strerror(errno));
147     return -1;
148     }
149
150 struct sockaddr_in addr;
151 addr.sin_family = AF_INET;
152 addr.sin_port = htons(rpcConfigSettings.GetPort());
153 addr.sin_addr.s_addr = inet_addr("0.0.0.0");
154
155 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)))
156     {
157     errorStr = "Failed to bind socket";
158     printfd(__FILE__, "Failed to bind listening socket: %s\n", strerror(errno));
159     return -1;
160     }
161
162 if (listen(fd, 10))
163     {
164     errorStr = "Failed to listen socket";
165     printfd(__FILE__, "Failed to listen listening socket: %s\n", strerror(errno));
166     return -1;
167     }
168
169 rpcServer = new xmlrpc_c::serverAbyss(
170         xmlrpc_c::serverAbyss::constrOpt()
171         .registryP(&rpcRegistry)
172         .logFileName("/var/log/stargazer_rpc.log")
173         .socketFd(fd)
174         );
175
176 if (pthread_create(&tid, NULL, Run, this))
177     {
178     errorStr = "Failed to create RPC thread";
179     printfd(__FILE__, "Failed to crate RPC thread\n");
180     return -1;
181     }
182
183 return 0;
184 }
185
186 int RPC_CONFIG::Stop()
187 {
188 running = false;
189 for (int i = 0; i < 5 && !stopped; ++i)
190     {
191     struct timespec ts = {0, 200000000};
192     nanosleep(&ts, NULL);
193     }
194
195 if (!stopped)
196     {
197     running = true;
198     printfd(__FILE__, "Failed to stop RPC thread\n");
199     errorStr = "Failed to stop RPC thread";
200     return -1;
201     }
202 else
203     {
204     pthread_join(tid, NULL);
205     }
206
207 close(fd);
208
209 return 0;
210 }
211
212 void * RPC_CONFIG::Run(void * rc)
213 {
214 sigset_t signalSet;
215 sigfillset(&signalSet);
216 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
217
218 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
219
220 config->stopped = false;
221 while (config->running)
222     {
223     if (WaitPackets(config->fd))
224         config->rpcServer->runOnce();
225     }
226 config->stopped = true;
227
228 return NULL;
229 }
230
231 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
232                               ADMIN_INFO * info)
233 {
234 std::map<std::string,
235          ADMIN_INFO>::iterator it;
236
237 it = cookies.find(cookie);
238
239 if (it == cookies.end())
240     {
241     return true;
242     }
243
244 if (difftime(it->second.accessTime, time(NULL)) >
245     rpcConfigSettings.GetCookieTimeout())
246     {
247     cookies.erase(it);
248     return true;
249     }
250
251 // Update access time
252 time(&it->second.accessTime);
253 *info = it->second;
254 return false;
255 }
256
257 bool RPC_CONFIG::CheckAdmin(const std::string & login,
258                             const std::string & password,
259                             std::string * cookie)
260 {
261 ADMIN * admin = NULL;
262
263 if (!admins->Correct(login, password, &admin))
264     {
265     return true;
266     }
267
268 ADMIN_INFO info;
269 time(&info.accessTime);
270 info.admin = login;
271 info.priviledges = *admin->GetPriv();
272 *cookie = GetCookie();
273 cookies[*cookie] = info;
274
275 return false;
276 }
277
278 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
279 {
280 std::map<std::string,
281          ADMIN_INFO>::iterator it;
282
283 it = cookies.find(cookie);
284
285 if (it == cookies.end())
286     {
287     return true;
288     }
289
290 cookies.erase(it);
291
292 return false;
293 }
294
295 std::string RPC_CONFIG::GetCookie() const
296 {
297 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
298 std::string cookie;
299
300 for (int i = 0; i < 64; ++i)
301     {
302     cookie += charset[rand() % charset.length()];
303     };
304
305 return cookie;
306 }
307
308 void RPC_CONFIG::InitiateRegistry()
309 {
310 // manage registry
311 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
312             tariffs,
313             users,
314             dayFee,
315             dirNames
316             ));
317 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
318
319 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
320             this
321             ));
322 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
323
324 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
325             this
326             ));
327 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
328
329 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
330             this,
331             users
332             ));
333 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
334
335 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
336             this,
337             admins,
338             users
339             ));
340 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
341
342 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
343             this,
344             admins,
345             users
346             ));
347 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
348
349 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
350             this,
351             users
352             ));
353 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
354
355 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
356             this,
357             admins,
358             tariffs,
359             store,
360             users
361             ));
362 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
363
364 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
365             this,
366             admins,
367             store,
368             users
369             ));
370 rpcRegistry.addMethod("stargazer.add_user_cash", methodAddCashPtr);
371
372 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
373             this,
374             admins,
375             store,
376             users
377             ));
378 rpcRegistry.addMethod("stargazer.set_user_cash", methodSetCashPtr);
379
380 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
381             this,
382             admins,
383             tariffs,
384             store,
385             users
386             ));
387 rpcRegistry.addMethod("stargazer.chg_user_tariff", methodTariffChangePtr);
388
389 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
390             this,
391             tariffs
392             ));
393 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
394
395 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
396             this,
397             admins,
398             tariffs
399             ));
400 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
401
402 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
403             this,
404             tariffs
405             ));
406 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
407
408 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
409             this,
410             admins,
411             tariffs
412             ));
413 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
414
415 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
416             this,
417             admins,
418             tariffs,
419             users
420             ));
421 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
422
423 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
424             this,
425             admins
426             ));
427 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
428
429 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
430             this,
431             admins
432             ));
433 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
434
435 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
436             this,
437             admins
438             ));
439 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
440
441 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
442             this,
443             admins
444             ));
445 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
446
447 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
448             this,
449             admins
450             ));
451 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
452
453 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
454             this,
455             users
456             ));
457 rpcRegistry.addMethod("stargazer.send_user_message", methodSendMessagePtr);
458
459 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
460             this,
461             users
462             ));
463 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);
464 }
465