2 #include <sys/socket.h>
3 #include <netinet/in.h>
13 #include <ostream> // xmlrpc-c devs have missed something :)
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"
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"
28 PLUGIN_CREATOR<RPC_CONFIG> rpcc;
30 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
37 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
41 std::vector<PARAM_VALUE>::const_iterator pvi;
44 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
45 if (pvi == s.moduleParams.end())
47 errorStr = "Parameter \'Port\' not found.";
48 printfd(__FILE__, "Parameter 'Port' not found\n");
51 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
53 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
54 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
59 pv.param = "CookieTimeout";
60 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
61 if (pvi == s.moduleParams.end())
63 cookieTimeout = 1800; // 30 * 60
67 if (str2x(pvi->value[0], cookieTimeout))
69 errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
70 printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
80 return rpcc.GetPlugin();
83 RPC_CONFIG::RPC_CONFIG()
100 logger(GetPluginLogger(GetStgLogger(), "conf_rpc"))
104 RPC_CONFIG::~RPC_CONFIG()
110 int RPC_CONFIG::ParseSettings()
112 int ret = rpcConfigSettings.ParseSettings(settings);
115 errorStr = rpcConfigSettings.GetStrError();
120 void RPC_CONFIG::SetStgSettings(const SETTINGS * settings)
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));
129 int RPC_CONFIG::Start()
134 fd = socket(AF_INET, SOCK_STREAM, 0);
137 errorStr = "Failed to create socket";
138 logger("Cannot create a socket: %s", strerror(errno));
139 printfd(__FILE__, "Failed to create listening socket: %s\n", strerror(errno));
145 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)))
147 errorStr = "Setsockopt failed.";
148 logger("setsockopt error: %s", strerror(errno));
149 printfd(__FILE__, "Setsockopt failed: %s\n", strerror(errno));
153 struct sockaddr_in addr;
154 addr.sin_family = AF_INET;
155 addr.sin_port = htons(rpcConfigSettings.GetPort());
156 addr.sin_addr.s_addr = inet_addr("0.0.0.0");
158 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)))
160 logger("Cannot bind the socket: %s", strerror(errno));
161 errorStr = "Failed to bind socket";
162 printfd(__FILE__, "Failed to bind listening socket: %s\n", strerror(errno));
168 logger("Cannot listen the socket: %s", strerror(errno));
169 errorStr = "Failed to listen socket";
170 printfd(__FILE__, "Failed to listen listening socket: %s\n", strerror(errno));
174 rpcServer = new xmlrpc_c::serverAbyss(
175 xmlrpc_c::serverAbyss::constrOpt()
176 .registryP(&rpcRegistry)
177 .logFileName("/var/log/stargazer_rpc.log")
181 if (pthread_create(&tid, NULL, Run, this))
183 errorStr = "Failed to create RPC thread";
184 logger("Cannot create RPC thread.");
185 printfd(__FILE__, "Failed to crate RPC thread\n");
192 int RPC_CONFIG::Stop()
195 for (int i = 0; i < 5 && !stopped; ++i)
197 struct timespec ts = {0, 200000000};
198 nanosleep(&ts, NULL);
204 logger("Cannot stop RPC thread.");
205 printfd(__FILE__, "Failed to stop RPC thread\n");
206 errorStr = "Failed to stop RPC thread";
211 pthread_join(tid, NULL);
219 void * RPC_CONFIG::Run(void * rc)
222 sigfillset(&signalSet);
223 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
225 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
227 config->stopped = false;
228 while (config->running)
230 if (WaitPackets(config->fd))
231 config->rpcServer->runOnce();
233 config->stopped = true;
238 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
241 std::map<std::string,
242 ADMIN_INFO>::iterator it;
244 it = cookies.find(cookie);
246 if (it == cookies.end())
251 if (difftime(it->second.accessTime, time(NULL)) >
252 rpcConfigSettings.GetCookieTimeout())
258 // Update access time
259 time(&it->second.accessTime);
264 bool RPC_CONFIG::CheckAdmin(const std::string & login,
265 const std::string & password,
266 std::string * cookie)
268 ADMIN * admin = NULL;
270 if (!admins->Correct(login, password, &admin))
272 logger("Attempt to connect with invalid credentials. Login: %s", login.c_str());
277 time(&info.accessTime);
279 info.priviledges = *admin->GetPriv();
280 *cookie = GetCookie();
281 cookies[*cookie] = info;
286 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
288 std::map<std::string,
289 ADMIN_INFO>::iterator it;
291 it = cookies.find(cookie);
293 if (it == cookies.end())
303 std::string RPC_CONFIG::GetCookie() const
305 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
308 for (int i = 0; i < 64; ++i)
310 cookie += charset[rand() % charset.length()];
316 void RPC_CONFIG::InitiateRegistry()
319 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
325 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
327 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
330 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
332 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
335 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
337 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
341 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
343 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
348 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
350 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
355 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
357 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
361 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
363 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
370 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
372 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
378 rpcRegistry.addMethod("stargazer.add_user_cash", methodAddCashPtr);
380 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
386 rpcRegistry.addMethod("stargazer.set_user_cash", methodSetCashPtr);
388 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
395 rpcRegistry.addMethod("stargazer.chg_user_tariff", methodTariffChangePtr);
397 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
401 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
403 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
408 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
410 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
414 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
416 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
421 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
423 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
429 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
431 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
435 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
437 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
441 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
443 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
447 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
449 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
453 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
455 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
459 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
461 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
465 rpcRegistry.addMethod("stargazer.send_user_message", methodSendMessagePtr);
467 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
471 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);