diff options
-rw-r--r-- | proto_lib/api_protocol.h | 16 | ||||
-rw-r--r-- | proto_lib/lib.project | 1 | ||||
-rw-r--r-- | proto_lib/packet.cpp | 45 | ||||
-rw-r--r-- | proto_lib/packet.h | 4 | ||||
-rw-r--r-- | proto_lib/utilities.cpp | 13 | ||||
-rw-r--r-- | proto_lib/utilities.h | 5 | ||||
-rw-r--r-- | restarter.workspace | 7 | ||||
-rw-r--r-- | server/api_service.h | 16 | ||||
-rw-r--r-- | server/headers.h | 2 | ||||
-rw-r--r-- | server/modules.cpp | 3 | ||||
-rw-r--r-- | server/restarter_server.project | 7 | ||||
-rw-r--r-- | server/server.cpp | 127 | ||||
-rw-r--r-- | server/server.h | 89 | ||||
-rw-r--r-- | server/service.cpp | 18 | ||||
-rw-r--r-- | services/unix_exec_service/main.cpp | 6 |
15 files changed, 148 insertions, 211 deletions
diff --git a/proto_lib/api_protocol.h b/proto_lib/api_protocol.h index 88195e2..2e42d42 100644 --- a/proto_lib/api_protocol.h +++ b/proto_lib/api_protocol.h @@ -17,6 +17,7 @@ #ifndef API_PROTOCOL_H #define API_PROTOCOL_H +namespace proto { struct service_s { @@ -28,8 +29,13 @@ struct service_s std::list<cmd> cmds; }; -enum packet_type {AUTH_REPLY, AUTH_REQUEST, SERVICES_REPLY, SERVICES_REQUEST, COMMAND_REPLY, COMMAND_REQUEST, UNKNOWN}; -enum status {failure = 0x00, success = 0x01}; +struct svc_cmd +{ + std::string service, command; +}; + +enum packet_type {TYPE_AUTH_REPLY, TYPE_AUTH_REQUEST, TYPE_SERVICES_REPLY, TYPE_SERVICES_REQUEST, TYPE_COMMAND_REPLY, TYPE_COMMAND_REQUEST, TYPE_CUSTOM, TYPE_UNKNOWN}; +enum status {STATUS_FAILURE = 0x00, STATUS_SUCCESS = 0x01}; class packet { @@ -47,7 +53,7 @@ public: //helper functions: //client functions - static packet *cli_make_auth_packet(); //should be first packet to server + static packet *cli_make_init_packet(); //should be first packet to server static packet *cli_make_command_packet(std::string &service, std::string &command); static packet *cli_make_request_services_packet(); @@ -58,6 +64,7 @@ public: static bool serv_validate_client_proto(packet&); //false on fail static packet *serv_make_services_packet(std::list<service_s>&); static packet *serv_make_command_reply_packet(std::string&, status s); + static svc_cmd serv_extract_command(packet&); //generic static packet *make_status_packet(packet_type type, status s); @@ -66,7 +73,6 @@ private: std::vector<unsigned char> data; }; - - +}; #endif diff --git a/proto_lib/lib.project b/proto_lib/lib.project index 39a0a37..65be5d8 100644 --- a/proto_lib/lib.project +++ b/proto_lib/lib.project @@ -73,6 +73,7 @@ <Environment EnvVarSetName="<Use Defaults>" DbgSetName="<Use Defaults>"> <![CDATA[ + ]]> </Environment> <Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath=""> diff --git a/proto_lib/packet.cpp b/proto_lib/packet.cpp index 8cf73b2..0ed3150 100644 --- a/proto_lib/packet.cpp +++ b/proto_lib/packet.cpp @@ -22,7 +22,8 @@ #include "packet.h" #include "utilities.h" - +namespace proto +{ packet::packet(std::vector<unsigned char>& new_data) { @@ -41,6 +42,11 @@ bool packet::assign(std::vector<unsigned char>& v) return is_good(); } +const std::vector<unsigned char> &packet::raw() +{ + return data; +} + bool packet::is_good() { if(data.empty()) @@ -88,12 +94,12 @@ packet_type packet::get_type() type.push_back(*i); bool cli = is_client_packet(); if(std::search(type.begin(), type.end(), type_auth, type_auth + sizeof(type_auth)) != type.end()) - return cli?AUTH_REQUEST:AUTH_REPLY; + return cli?TYPE_AUTH_REQUEST:TYPE_AUTH_REPLY; if(std::search(type.begin(), type.end(), type_services, type_services + sizeof(type_services)) != type.end()) - return cli?SERVICES_REQUEST:SERVICES_REPLY; + return cli?TYPE_SERVICES_REQUEST:TYPE_SERVICES_REPLY; if(std::search(type.begin(), type.end(), type_command, type_command + sizeof(type_command)) != type.end()) - return cli?COMMAND_REQUEST:COMMAND_REPLY; - return UNKNOWN; + return cli?TYPE_COMMAND_REQUEST:TYPE_COMMAND_REPLY; + return TYPE_UNKNOWN; } bool packet::is_status_packet() @@ -101,13 +107,13 @@ bool packet::is_status_packet() const unsigned char *type = to_internal_type(get_type()); std::vector<unsigned char>::iterator i = std::search(data.begin(), data.end(), type, type + sizeof(type)); i += sizeof(type); - if(*i == success || *i == failure) + if(*i == STATUS_SUCCESS || *i == STATUS_FAILURE) return true; return false; } -packet *packet::cli_make_auth_packet() +packet *packet::cli_make_init_packet() { std::vector<unsigned char> v; pack_cli_header(v); @@ -292,8 +298,31 @@ bool packet::check_status() if(i != data.end()) { i += sizeof(type); - if(*i == success) + if(*i == STATUS_SUCCESS) return true; } return false; } + +svc_cmd packet::serv_extract_command(packet& p) +{ + svc_cmd c; + std::vector<unsigned char>::const_iterator i = std::search(p.raw().begin(), p.raw().end(), type_command, type_command + sizeof(type_command)); + if(i != p.raw().end()) + { + i+= sizeof(type_command); + std::vector<unsigned char>::const_iterator i2 = std::find(i, p.raw().end(), delimiter); + if(i2 != p.raw().end()) + { + for(; i < i2; ++i) + c.service.push_back((char)*i); + i++; + i2 = std::search(p.raw().begin(), p.raw().end(), proto_footer, proto_footer + sizeof(proto_footer)); + for(; i < i2; ++i) + c.command.push_back((char)*i); + } + } + return c; +} + +}; diff --git a/proto_lib/packet.h b/proto_lib/packet.h index 58422cc..b431840 100644 --- a/proto_lib/packet.h +++ b/proto_lib/packet.h @@ -17,6 +17,8 @@ #ifndef PACKET_H #define PACKET_H +namespace proto +{ const unsigned char proto_header [] = { 0x06, 0x06, 0x06, 0x13}; const unsigned char proto_footer [] = { 0x13, 0x06, 0x06, 0x06}; @@ -38,4 +40,6 @@ const unsigned char type_auth [] = {0x01, 0x01}; const unsigned char type_services [] = {0xA, 0x01}; const unsigned char type_command [] = {0xA, 0x02}; +}; + #endif diff --git a/proto_lib/utilities.cpp b/proto_lib/utilities.cpp index befa203..64f69bb 100644 --- a/proto_lib/utilities.cpp +++ b/proto_lib/utilities.cpp @@ -21,6 +21,9 @@ #include <string.h> #include "utilities.h" +namespace proto +{ + void pack_cli_header(std::vector<unsigned char> &v) { pack_buffer(proto_header, sizeof(proto_header), v); @@ -63,17 +66,19 @@ const unsigned char *to_internal_type(packet_type t) const unsigned char *type = NULL; switch(t) { - case AUTH_REPLY: case AUTH_REQUEST: + case TYPE_AUTH_REPLY: case TYPE_AUTH_REQUEST: type = type_auth; break; - case SERVICES_REPLY: case SERVICES_REQUEST: + case TYPE_SERVICES_REPLY: case TYPE_SERVICES_REQUEST: type = type_services; break; - case COMMAND_REPLY: case COMMAND_REQUEST: + case TYPE_COMMAND_REPLY: case TYPE_COMMAND_REQUEST: type = type_command; break; - case UNKNOWN: default: + case TYPE_UNKNOWN: default: break; }; return type; } + +}; diff --git a/proto_lib/utilities.h b/proto_lib/utilities.h index 7b8713b..6b41ca3 100644 --- a/proto_lib/utilities.h +++ b/proto_lib/utilities.h @@ -20,6 +20,9 @@ #ifndef UTILITIES_H #define UTILITIES_H +namespace proto +{ + void pack_cli_header(std::vector<unsigned char> &v); void pack_serv_header(std::vector<unsigned char> &v); void pack_buffer(const unsigned char *buf, size_t len, std::vector<unsigned char> &v); @@ -27,6 +30,6 @@ void pack_buffer(std::vector<unsigned char> &source, std::vector<unsigned char> void pack_buffer(std::string &str, std::vector<unsigned char> &v); void pack_buffer(const char* str, std::vector<unsigned char> &v); const unsigned char *to_internal_type(packet_type t); - +}; #endif diff --git a/restarter.workspace b/restarter.workspace index eea7ad7..79a199e 100644 --- a/restarter.workspace +++ b/restarter.workspace @@ -1,8 +1,8 @@ <?xml version="1.0" encoding="utf-8"?> <CodeLite_Workspace Name="restarter" Database="./restarter.tags"> <Project Name="restarter_server" Path="server/restarter_server.project" Active="No"/> - <Project Name="lib" Path="proto_lib/lib.project" Active="Yes"/> - <Project Name="unix_exec_service" Path="services/unix_exec_service/unix_exec_service.project" Active="No"/> + <Project Name="lib" Path="proto_lib/lib.project" Active="No"/> + <Project Name="unix_exec_service" Path="services/unix_exec_service/unix_exec_service.project" Active="Yes"/> <BuildMatrix> <WorkspaceConfiguration Name="Debug" Selected="yes"> <Project Name="restarter_server" ConfigName="Debug"/> @@ -16,6 +16,7 @@ </WorkspaceConfiguration> </BuildMatrix> <Environment> - <![CDATA[]]> + <![CDATA[ + ]]> </Environment> </CodeLite_Workspace> 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="<Use Defaults>" DbgSetName="<Use Defaults>"> - <![CDATA[]]> + <![CDATA[ + ]]> </Environment> <Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath=""> <PostConnectCommands/> @@ -94,6 +98,7 @@ <Environment EnvVarSetName="<Use Defaults>" DbgSetName="<Use Defaults>"> <![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; +} + +}; diff --git a/services/unix_exec_service/main.cpp b/services/unix_exec_service/main.cpp index ea7faef..ad7da1f 100644 --- a/services/unix_exec_service/main.cpp +++ b/services/unix_exec_service/main.cpp @@ -24,6 +24,8 @@ #include "api_service.h" +using namespace service; + struct alias { std::string _alias, cmd; @@ -34,7 +36,7 @@ struct alias }; std::list<alias> aliases; -std::list<command> cmds; +std::list<service_info::command> cmds; std::string extract_aliased_cmd(std::string cmd) { @@ -104,7 +106,7 @@ void load_cmds() a.cmd = cmd; aliases.push_back(a); } - command c; + service_info::command c; c.command = cmd; c.description = descr; cmds.push_back(c); |