summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/api_service.h16
-rw-r--r--server/headers.h2
-rw-r--r--server/modules.cpp3
-rw-r--r--server/restarter_server.project7
-rw-r--r--server/server.cpp127
-rw-r--r--server/server.h89
-rw-r--r--server/service.cpp18
7 files changed, 74 insertions, 188 deletions
diff --git a/server/api_service.h b/server/api_service.h
index 9d6a656..4f5a183 100644
--- a/server/api_service.h
+++ b/server/api_service.h
@@ -17,23 +17,29 @@
#ifndef API_SERVICES_H
#define API_SERVICES_H
+namespace service
+
+{
+
enum service_return {RET_NONE = 2, RET_VOID_PTR = 4, RET_CHAR_PTR = 8, RET_STD_STRING = 16, RET_INT, RET_FLOAT = 32};
enum service_accept {ACC_NONE = 2, ACC_VOID_PTR = 4, ACC_CHAR_PTR = 8, ACC_STD_STRING = 16, ACC_INT, ACC_FLOAT = 32};
-struct command
-{
- std::string command, description;
-};
//service may have predefined command set
struct service_info
{
+ struct command
+ {
+ std::string command, description;
+ bool operator==(const std::string &command);
+ };
std::string name, description;
service_return ret;
service_accept acc;
void * (*exec)(void *);
std::list<command> predefined;
+ bool operator==(const std::string &name);
};
#define services(x) extern "C" void __attribute__((__visibility__("default"))) x(std::list<service_info> &s)
@@ -42,7 +48,7 @@ struct service_info
#define init_func(x) extern "C" void __attribute__((__visibility__("default"))) x()
//service may export "init" function which will be called on service loading
-
+};
#endif
diff --git a/server/headers.h b/server/headers.h
index c7119b6..0f0587a 100644
--- a/server/headers.h
+++ b/server/headers.h
@@ -24,3 +24,5 @@
#include "get_function.h"
#include "modules.h"
#include "server.h"
+#include "session.h"
+#include "service.h"
diff --git a/server/modules.cpp b/server/modules.cpp
index 1f10b90..f042d82 100644
--- a/server/modules.cpp
+++ b/server/modules.cpp
@@ -16,7 +16,8 @@
#include "headers.h"
-std::list<service_info> installed_services;
+using namespace service;
+
void load_modules(const char* path)
{
diff --git a/server/restarter_server.project b/server/restarter_server.project
index d2e0e61..f3bd51b 100644
--- a/server/restarter_server.project
+++ b/server/restarter_server.project
@@ -20,6 +20,9 @@
<File Name="modules.h"/>
<File Name="server.cpp"/>
<File Name="server.h"/>
+ <File Name="session.cpp"/>
+ <File Name="session.h"/>
+ <File Name="service.h"/>
</VirtualDirectory>
<Dependencies Name="Debug"/>
<Dependencies Name="Release"/>
@@ -50,7 +53,8 @@
<ResourceCompiler Options="" Required="no"/>
<General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Debug" Command="./$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes"/>
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
- <![CDATA[]]>
+ <![CDATA[
+ ]]>
</Environment>
<Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath="">
<PostConnectCommands/>
@@ -94,6 +98,7 @@
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
<![CDATA[
+
]]>
</Environment>
<Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath="">
diff --git a/server/server.cpp b/server/server.cpp
index bdd2cfb..7a13fe6 100644
--- a/server/server.cpp
+++ b/server/server.cpp
@@ -17,113 +17,42 @@
#include "headers.h"
-void session::handle_handshake(const boost::system::error_code& error)
+
+
+server::server(boost::asio::io_service& io_service, short port) :
+io_service_(io_service), acceptor_(io_service, tcp::endpoint(boost::asio::ip::address_v4(), port)), context_(boost::asio::ssl::context::sslv23)
+{
+ context_.set_options(boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2);
+ context_.set_password_callback(boost::bind(&server::get_password, this));
+ context_.use_certificate_chain_file("/etc/restarter_server/serv.crt");
+ context_.use_rsa_private_key_file("/etc/restarter_server/serv.key", boost::asio::ssl::context::pem);
+ context_.load_verify_file("/etc/restarter_server/ca.crt");
+ context_.set_verify_mode(boost::asio::ssl::verify_peer | boost::asio::ssl::verify_client_once);
+ start_accept();
+}
+
+
+void server::start_accept()
+{
+ session* new_session = new session(io_service_, context_);
+ acceptor_.async_accept(new_session->socket(), boost::bind(&server::handle_accept, this, new_session, boost::asio::placeholders::error));
+}
+
+
+void server::handle_accept(session* new_session, const boost::system::error_code& error)
{
if (!error)
{
- socket_.async_read_some(boost::asio::buffer(data_, max_length), boost::bind(&session::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
+ new_session->start();
}
else
{
- delete this;
+ delete new_session;
}
+ start_accept();
}
-void session::handle_read(const boost::system::error_code& error, size_t bytes_transferred)
+std::string server::get_password() const //TODO: add key password support
{
- if (!error)
- {
-// std::cout<<"recieved: "<<data_<<"\n";
- if(strstr(data_, "restart vbox"))
- {
- FILE *f = popen("/sbin/runscript /etc/init.d/vbox_headles restart --nodeps","r");
- if(f != NULL)
- {
- char buf[128];
- while(fgets(buf, 128, f) != NULL)
- ; //TODO: do something with output
- int s = pclose(f); // TODO: handle exit status
- }
- else
- ; //TODO: handle fail
- }
- else if(strstr(data_, "reboot now"))
- {
- FILE *f = popen("reboot","r");
- if(f != NULL)
- {
- char buf[128];
- while(fgets(buf, 128, f) != NULL)
- ; //TODO: do something with output
- int s = pclose(f); // TODO: handle exit status
- }
- else
- ; //TODO: handle fail
- }
- else if(strstr(data_, "halt now"))
- {
- FILE *f = popen("halt","r");
- if(f != NULL)
- {
- char buf[128];
- while(fgets(buf, 128, f) != NULL)
- ; //TODO: do something with output
- int s = pclose(f); // TODO: handle exit status
- }
- else
- ; //TODO: handle fail
- }
- else if(strstr(data_, "restart cups"))
- {
- FILE *f = popen("/sbin/runscript /etc/init.d/cupsd restart --nodeps","r");
- if(f != NULL)
- {
- char buf[128];
- while(fgets(buf, 128, f) != NULL)
- ; //TODO: do something with output
- int s = pclose(f); // TODO: handle exit status
- }
- else
- ; //TODO: handle fail
- }
- else if(strstr(data_, "restart ppp"))
- {
- FILE *f = popen("/sbin/runscript /etc/init.d/net.ppp0 stop --nodeps","r");
- char buf[128];
- int s = 0;
- if(f != NULL)
- {
- while(fgets(buf, 128, f) != NULL)
- ; //TODO: do something with output
- s = pclose(f); // TODO: handle exit status
- }
- else
- ; //TODO: handle fail
- sleep(3);
- f = popen("killall pppd","r");
- if(f == NULL)
- {
- while(fgets(buf, 128, f) != NULL)
- ; //TODO: do something with output
- s = pclose(f); // TODO: handle exit status
- }
- else
- ; //TODO: handle fail
- sleep(1);
- f = popen("/sbin/runscript /etc/init.d/net.ppp0 start --nodeps","r");
- if(f == NULL)
- {
- while(fgets(buf, 128, f) != NULL)
- ; //TODO: do something with output
- s = pclose(f); // TODO: handle exit status
- }
- else
- ; //TODO: handle fail
- }
-/* boost::asio::async_write(socket_,
- boost::asio::buffer(data_, bytes_transferred),
- boost::bind(&session::handle_write, this,
- boost::asio::placeholders::error)); */
- }
- delete this;
+ return "";
}
diff --git a/server/server.h b/server/server.h
index c9028d3..4bc5e31 100644
--- a/server/server.h
+++ b/server/server.h
@@ -23,96 +23,21 @@ using boost::asio::ip::tcp;
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
-class session
-{
-public:
- session(boost::asio::io_service& io_service, boost::asio::ssl::context& context) : socket_(io_service, context)
- {}
-
- ssl_socket::lowest_layer_type& socket()
- {
- return socket_.lowest_layer();
- }
- void handle_handshake(const boost::system::error_code& error);
-
- void start()
- {
- socket_.async_handshake(boost::asio::ssl::stream_base::server, boost::bind(&session::handle_handshake, this, boost::asio::placeholders::error));
- }
-
-private:
- void handle_read(const boost::system::error_code& error, size_t bytes_transferred);
-
-/* void handle_write(const boost::system::error_code& error)
- {
- if (!error)
- {
- socket_.async_read_some(boost::asio::buffer(data_, max_length),
- boost::bind(&session::handle_read, this,
- boost::asio::placeholders::error,
- boost::asio::placeholders::bytes_transferred));
- }
- else
- {
- delete this;
- }
- }*/
-
- ssl_socket socket_;
- enum { max_length = 128 };
- char data_[max_length];
-};
class server
{
public:
- server(boost::asio::io_service& io_service, short port)
- : io_service_(io_service),
- acceptor_(io_service, tcp::endpoint(boost::asio::ip::address_v4(), port)),
- context_(boost::asio::ssl::context::sslv23)
- {
- context_.set_options(
- boost::asio::ssl::context::default_workarounds
- | boost::asio::ssl::context::no_sslv2);
- context_.set_password_callback(boost::bind(&server::get_password, this));
- context_.use_certificate_chain_file("/etc/restarter_server/serv.crt");
- context_.use_rsa_private_key_file("/etc/restarter_server/serv.key", boost::asio::ssl::context::pem);
- context_.load_verify_file("/etc/restarter_server/ca.crt");
- context_.set_verify_mode(boost::asio::ssl::verify_peer | boost::asio::ssl::verify_client_once);
- start_accept();
- }
+ server(boost::asio::io_service& io_service, short port);
private:
- void start_accept()
- {
- session* new_session = new session(io_service_, context_);
- acceptor_.async_accept(new_session->socket(),
- boost::bind(&server::handle_accept, this, new_session,
- boost::asio::placeholders::error));
- }
- std::string get_password() const
- {
- return "";
- }
-
- void handle_accept(session* new_session,
- const boost::system::error_code& error)
- {
- if (!error)
- {
- new_session->start();
- }
- else
- {
- delete new_session;
- }
+ void start_accept();
+ std::string get_password() const;
- start_accept();
- }
+ void handle_accept(session* new_session, const boost::system::error_code& error);
- boost::asio::io_service& io_service_;
- boost::asio::ip::tcp::acceptor acceptor_;
- boost::asio::ssl::context context_;
+ boost::asio::io_service& io_service_;
+ boost::asio::ip::tcp::acceptor acceptor_;
+ boost::asio::ssl::context context_;
};
#endif
diff --git a/server/service.cpp b/server/service.cpp
index ab9d7a1..004def3 100644
--- a/server/service.cpp
+++ b/server/service.cpp
@@ -14,3 +14,21 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#include "headers.h"
+
+namespace service
+{
+
+ std::list<service_info> installed_services;
+
+bool service_info::operator==(const std::string &name)
+{
+ return name == this->name;
+}
+
+bool service_info::command::operator==(const std::string &command)
+{
+ return command == this->command;
+}
+
+};