3 #include "info_methods.h"
4 #include "users_methods.h"
5 #include "tariffs_methods.h"
6 #include "admins_methods.h"
7 #include "messages_methods.h"
9 #include "stg/common.h"
10 #include "stg/admin.h"
11 #include "stg/module_settings.h"
12 #include "stg/settings.h"
16 #include <ostream> // xmlrpc-c devs have missed something :)
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
28 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
34 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
38 std::vector<PARAM_VALUE>::const_iterator pvi;
39 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
40 if (pvi == s.moduleParams.end() || pvi->value.empty())
42 errorStr = "Parameter \'Port\' not found.";
43 printfd(__FILE__, "Parameter 'Port' not found\n");
47 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
49 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
50 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
53 port = static_cast<uint16_t>(p);
55 pv.param = "CookieTimeout";
56 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
57 if (pvi == s.moduleParams.end() || pvi->value.empty())
59 cookieTimeout = 1800; // 30 * 60
63 if (str2x(pvi->value[0], cookieTimeout))
65 errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
66 printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
74 extern "C" Plugin* GetPlugin()
76 static RPC_CONFIG plugin;
80 RPC_CONFIG::RPC_CONFIG()
90 logger(PluginLogger::get("conf_rpc"))
94 RPC_CONFIG::~RPC_CONFIG()
100 int RPC_CONFIG::ParseSettings()
102 int ret = rpcConfigSettings.ParseSettings(settings);
105 errorStr = rpcConfigSettings.GetStrError();
110 void RPC_CONFIG::SetStgSettings(const SETTINGS * s)
112 dayFee = s->GetDayFee();
113 dirNames.erase(dirNames.begin(), dirNames.end());
114 for (size_t i = 0; i < DIR_NUM; ++i) {
115 dirNames.push_back(s->GetDirName(i));
119 int RPC_CONFIG::Start()
124 fd = socket(AF_INET, SOCK_STREAM, 0);
127 errorStr = "Failed to create socket";
128 logger("Cannot create a socket: %s", strerror(errno));
129 printfd(__FILE__, "Failed to create listening socket: %s\n", strerror(errno));
135 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)))
137 errorStr = "Setsockopt failed.";
138 logger("setsockopt error: %s", strerror(errno));
139 printfd(__FILE__, "Setsockopt failed: %s\n", strerror(errno));
143 struct sockaddr_in addr;
144 addr.sin_family = AF_INET;
145 addr.sin_port = htons(rpcConfigSettings.GetPort());
146 addr.sin_addr.s_addr = inet_addr("0.0.0.0");
148 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)))
150 logger("Cannot bind the socket: %s", strerror(errno));
151 errorStr = "Failed to bind socket";
152 printfd(__FILE__, "Failed to bind listening socket: %s\n", strerror(errno));
158 logger("Cannot listen the socket: %s", strerror(errno));
159 errorStr = "Failed to listen socket";
160 printfd(__FILE__, "Failed to listen listening socket: %s\n", strerror(errno));
164 rpcServer = new xmlrpc_c::serverAbyss(
165 xmlrpc_c::serverAbyss::constrOpt()
166 .registryP(&rpcRegistry)
167 .logFileName("/var/log/stargazer_rpc.log")
171 if (pthread_create(&tid, NULL, Run, this))
173 errorStr = "Failed to create RPC thread";
174 logger("Cannot create RPC thread.");
175 printfd(__FILE__, "Failed to crate RPC thread\n");
182 int RPC_CONFIG::Stop()
185 for (int i = 0; i < 5 && !stopped; ++i)
187 struct timespec ts = {0, 200000000};
188 nanosleep(&ts, NULL);
194 logger("Cannot stop RPC thread.");
195 printfd(__FILE__, "Failed to stop RPC thread\n");
196 errorStr = "Failed to stop RPC thread";
201 pthread_join(tid, NULL);
209 void * RPC_CONFIG::Run(void * rc)
212 sigfillset(&signalSet);
213 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
215 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
217 config->stopped = false;
218 while (config->running)
220 if (WaitPackets(config->fd))
221 config->rpcServer->runOnce();
223 config->stopped = true;
228 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
231 std::map<std::string,
232 ADMIN_INFO>::iterator it;
234 it = cookies.find(cookie);
236 if (it == cookies.end())
241 if (difftime(it->second.accessTime, time(NULL)) >
242 rpcConfigSettings.GetCookieTimeout())
248 // Update access time
249 time(&it->second.accessTime);
254 bool RPC_CONFIG::CheckAdmin(const std::string & login,
255 const std::string & password,
256 std::string * cookie)
258 ADMIN * admin = NULL;
260 if (!admins->Correct(login, password, &admin))
262 logger("Attempt to connect with invalid credentials. Login: %s", login.c_str());
267 time(&info.accessTime);
269 info.priviledges = *admin->GetPriv();
270 *cookie = GetCookie();
271 cookies[*cookie] = info;
276 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
278 std::map<std::string,
279 ADMIN_INFO>::iterator it;
281 it = cookies.find(cookie);
283 if (it == cookies.end())
293 std::string RPC_CONFIG::GetCookie() const
295 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
298 for (int i = 0; i < 64; ++i)
300 cookie += charset[rand() % charset.length()];
306 void RPC_CONFIG::InitiateRegistry()
309 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
315 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
317 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
320 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
322 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
325 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
327 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
331 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
333 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
338 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
340 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
345 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
347 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
351 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
353 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
360 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
362 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
368 rpcRegistry.addMethod("stargazer.add_user_cash", methodAddCashPtr);
370 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
376 rpcRegistry.addMethod("stargazer.set_user_cash", methodSetCashPtr);
378 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
385 rpcRegistry.addMethod("stargazer.chg_user_tariff", methodTariffChangePtr);
387 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
391 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
393 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
398 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
400 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
404 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
406 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
411 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
413 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
419 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
421 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
425 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
427 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
431 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
433 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
437 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
439 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
443 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
445 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
449 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
451 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
455 rpcRegistry.addMethod("stargazer.send_user_message", methodSendMessagePtr);
457 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
461 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);
463 xmlrpc_c::methodPtr const methodGetUserAuthByPtr(new METHOD_GET_USER_AUTH_BY(
467 rpcRegistry.addMethod("stargazer.get_user_auth_by", methodGetUserAuthByPtr);