diff --git a/ServidorTFG.pro b/ServidorTFG.pro index bab0040..640cd41 100644 --- a/ServidorTFG.pro +++ b/ServidorTFG.pro @@ -7,7 +7,6 @@ SOURCES += \ main.cpp \ conexion.cpp \ conexion_ssl.cpp \ - thread_selector.cpp \ config_reader.cpp \ tests.cpp \ data_acces.cpp \ @@ -19,7 +18,6 @@ SOURCES += \ HEADERS += \ conexion.h \ conexion_ssl.h \ - thread_selector.h \ config_reader.h \ tests.h \ data_acces.h \ diff --git a/ServidorTFG.pro.user b/ServidorTFG.pro.user index 40226cc..adb031d 100644 --- a/ServidorTFG.pro.user +++ b/ServidorTFG.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/conexion.cpp b/conexion.cpp index 76c03c6..3f70b6d 100644 --- a/conexion.cpp +++ b/conexion.cpp @@ -56,7 +56,6 @@ void conexion::start_server(){ int client = accept(sock, (struct sockaddr*)&addr, &len); std::thread t_client(conexion_client , client); t_client.detach(); - //cont++; } close(sock); } @@ -70,8 +69,13 @@ void conexion_client(int client){ exit(EXIT_FAILURE); }else{ session_manager* session = new session_manager(client); - while(!session->validate_pass()); - session->start_dialog(); + bool enter=session->validate_pass(); + while(!enter){ + enter=session->validate_pass(); + } + if(enter){ + session->start_dialog(); + } delete (session); close(client); } diff --git a/conexion_ssl.cpp b/conexion_ssl.cpp index ede114e..b8ff96b 100644 --- a/conexion_ssl.cpp +++ b/conexion_ssl.cpp @@ -29,7 +29,6 @@ void conexion_ssl::start_server(){ perror("bad port in config file"); } sock = this->create_socket(atoi(port.data())); - /* Handle connections */ SSL_CTX *ctx; init_openssl(); @@ -37,8 +36,6 @@ void conexion_ssl::start_server(){ this->configure_context(ctx); - //std::thread *hilos=new thread[50]; - //int cont=0; while(1) { struct sockaddr_in addr; uint len = sizeof(addr); @@ -46,7 +43,6 @@ void conexion_ssl::start_server(){ int client = accept(sock, (struct sockaddr*)&addr, &len); std::thread t_client(conexion_client,ctx , client); t_client.detach(); - //cont++; } close(sock); SSL_CTX_free(ctx); @@ -107,8 +103,6 @@ void conexion_ssl::configure_context(SSL_CTX *ctx) } void conexion_client(SSL_CTX *ctx,int client){ - char buf [256]; - if (client < 0) { perror("Unable to accept"); exit(EXIT_FAILURE); @@ -122,9 +116,13 @@ void conexion_client(SSL_CTX *ctx,int client){ } else { session_manager* session = new session_manager_ssl(ssl); - while(!session->validate_pass()); - session->start_dialog(); - //SSL_write(ssl,std::to_string(la->execute()).data() , sizeof (int)); + bool enter=session->validate_pass(); + while(!enter){ + enter=session->validate_pass(); + } + if(enter){ + session->start_dialog(); + } delete (session); } SSL_shutdown(ssl); diff --git a/data_acces.cpp b/data_acces.cpp index a5e8533..04745c2 100644 --- a/data_acces.cpp +++ b/data_acces.cpp @@ -1,6 +1,18 @@ #include "data_acces.h" +#include +#include +#include data_acces::data_acces() { } + +char* data_acces::get_hash(char *data){ + unsigned char hash[SHA512_DIGEST_LENGTH]; + SHA512(reinterpret_cast(data), strlen(data), hash); + unsigned char* encodedData= new unsigned char[200]; + EVP_EncodeBlock(encodedData, hash, sizeof (hash)); + return reinterpret_cast(encodedData); +} + diff --git a/data_acces.h b/data_acces.h index c0525f5..fa59c5f 100644 --- a/data_acces.h +++ b/data_acces.h @@ -17,6 +17,6 @@ public: virtual bool get_package_exists(std::string package)=0; virtual void create_user(std::string user, std::string pass, bool admin)=0; virtual void remove_user(std::string user)=0; + static char* get_hash(char *data); }; - #endif // DATA_ACCES_H diff --git a/main.cpp b/main.cpp index 7cb40cf..07723a4 100644 --- a/main.cpp +++ b/main.cpp @@ -5,11 +5,15 @@ #include "conexion_ssl.h" #include "config_reader.h" #include "tests.h" +#include "msql_acces.h" #define file_config "config" CPPUNIT_TEST_SUITE_REGISTRATION( test_basic ); int main() { + //msql_acces data; + //data.create_user("otro", "otro", true); + //inicio de los tests CPPUNIT_NS::TestResult testresult; CPPUNIT_NS::TestResultCollector collectedresults; diff --git a/msql_acces.cpp b/msql_acces.cpp index 7559160..2d596d3 100644 --- a/msql_acces.cpp +++ b/msql_acces.cpp @@ -20,6 +20,7 @@ std::string msql_acces::get_passwd(std::string username){ while(res->next()) ret = res->getString("passwd"); delete res; + delete pstmt; return ret; } @@ -31,6 +32,7 @@ bool msql_acces::get_admin(std::string username){ while(res->next()) ret = res->getBoolean("admin"); delete res; + delete pstmt; return ret; } @@ -55,6 +57,7 @@ std::list msql_acces::get_pinfo(){ ret.push_back(aux); } delete res; + delete pstmt; return ret; } @@ -76,6 +79,7 @@ std::list msql_acces::get_uinfo(){ ret.push_back(aux); } delete res; + delete pstmt; return ret; } @@ -85,6 +89,7 @@ void msql_acces::write_install(std::string package, std::string user){ pstmt->setString(1,package); pstmt->setString(2,user); pstmt->executeUpdate(); + delete pstmt; } void msql_acces::write_remove(std::string package){ @@ -92,6 +97,7 @@ void msql_acces::write_remove(std::string package){ con->prepareStatement("delete from packages where name=?"); pstmt->setString(1,package); pstmt->executeUpdate(); + delete pstmt; } bool msql_acces::get_package_exists(std::string package){ @@ -99,22 +105,20 @@ bool msql_acces::get_package_exists(std::string package){ pstmt->setString(1,package); sql::ResultSet *res = pstmt->executeQuery(); int ret=0; - while(res->next()) + while(res->next()){ ret = res->getInt(1); - + } + delete pstmt; return ret>0; } void msql_acces::create_user(std::string user, std::string pass, bool admin){ sql::PreparedStatement *pstmt = con->prepareStatement("insert into users(username, passwd, admin) values(?, ?, ?)"); - unsigned char hash[SHA512_DIGEST_LENGTH]; - SHA512(reinterpret_cast(pass.data()), strlen(pass.data()), hash); - unsigned char encodedData[200]; - EVP_EncodeBlock(encodedData, hash, sizeof (hash)); pstmt->setString(1,user); - pstmt->setString(2,std::string(reinterpret_cast(encodedData))); + pstmt->setString(2,std::string(data_acces::get_hash(&pass[0]))); pstmt->setBoolean(3,admin); pstmt->executeQuery(); + delete pstmt; } void msql_acces::remove_user(std::string user){ @@ -122,4 +126,5 @@ void msql_acces::remove_user(std::string user){ con->prepareStatement("delete from users where username=?"); pstmt->setString(1,user); pstmt->executeUpdate(); + delete pstmt; } diff --git a/session_manager.cpp b/session_manager.cpp index c870743..422cf7b 100644 --- a/session_manager.cpp +++ b/session_manager.cpp @@ -19,12 +19,8 @@ bool session_manager::validate_pass(){ this->read_data(buffer, 256); std::string user=buffer; this->read_data(buffer, 256); - // std::string pass=buffer; - unsigned char hash[SHA512_DIGEST_LENGTH]; - SHA512(reinterpret_cast(buffer), strlen(buffer), hash); - unsigned char encodedData[200]; - EVP_EncodeBlock(encodedData, hash, sizeof (hash)); - std::string pass=std::string(reinterpret_cast(encodedData)); + std::string pass=std::string(data_acces::get_hash(buffer)); + delete[] (buffer); if(this->data->get_passwd(user)==pass){ this->write_data("pass"); if(this->data->get_admin(user)){ @@ -62,6 +58,7 @@ void session_manager::start_dialog(){ break; } } + delete[] (buffer); } int session_manager::execute(){ @@ -75,11 +72,13 @@ int session_manager::execute(){ this->read_data(use_conf,256); conf.change_uses(use_conf); }else if(strcmp(use_conf,"n")!=0){ + delete[] (n_package); perror("fail in protocol comunication"); return -1; } delete [] (use_conf); std::string result = this->appli_command("--ask", n_package); + delete[] (n_package); if(result=="err"){ return -1; }else{ @@ -94,6 +93,7 @@ int session_manager::remove(){ char* n_package = new char[256]; this->read_data(n_package, 256); std::string result = this->appli_command("--unmerge",n_package); + delete[] (n_package); if(result=="err"){ return -1; }else{ @@ -121,10 +121,8 @@ std::string session_manager::appli_command(char comand[], char* n_package){ if(status>0){ this->write_data("ok"); ret = n_package; - delete[] (n_package); }else{ this->write_data("bad"); - delete[] (n_package); ret = "err"; } } @@ -167,6 +165,7 @@ void session_manager::remove_user(){ this->read_data(buffer,256); this->read_data(buffer,256); this->data->remove_user(std::string(buffer)); + delete[] (buffer); } int session_manager::read_data(char* input, int size){ diff --git a/tests.cpp b/tests.cpp index 861c506..69e522c 100644 --- a/tests.cpp +++ b/tests.cpp @@ -1,10 +1,15 @@ #include "tests.h" +#include "msql_acces.h" +#include +#include +#include void test_basic::setUp(){ this->conf=new config_reader("config"); + this->data=new msql_acces(); } -void test_basic::testInitial(){ +void test_basic::test_initial(){ std::string res; CPPUNIT_ASSERT(true==this->conf->get_param("port",res)); CPPUNIT_ASSERT("4433"==res); @@ -20,3 +25,23 @@ void test_basic::pass_tests(){ testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ()); testrunner.run(testresult); } + +void test_basic::hash_test(){ + CPPUNIT_ASSERT(strcmp(data_acces::get_hash("test1") + ,data_acces::get_hash("test1"))==0); + CPPUNIT_ASSERT(strcmp(data_acces::get_hash("test1") + ,data_acces::get_hash("test2"))!=0); +} + +void test_basic::msql_loggin_test(){ + std::string pass = this->data->get_passwd("test"); + CPPUNIT_ASSERT(pass==std::string(data_acces::get_hash("ok"))); + CPPUNIT_ASSERT(pass!=std::string(data_acces::get_hash("fail"))); +} + +void test_basic::msql_package_test(){ + data->write_install("p1","test"); + CPPUNIT_ASSERT(data->get_package_exists("p1")); + data->write_remove("p1"); + CPPUNIT_ASSERT(!data->get_package_exists("p1")); +} diff --git a/tests.h b/tests.h index 0e6397b..b96c911 100644 --- a/tests.h +++ b/tests.h @@ -12,17 +12,25 @@ #include #include #include "config_reader.h" +#include "data_acces.h" using namespace CppUnit; class test_basic : public CppUnit::TestFixture{ CPPUNIT_TEST_SUITE(test_basic); - CPPUNIT_TEST(testInitial); + CPPUNIT_TEST(test_initial); + CPPUNIT_TEST(hash_test); + CPPUNIT_TEST(msql_loggin_test); + CPPUNIT_TEST(msql_package_test); CPPUNIT_TEST_SUITE_END(); public: void setUp(void); void pass_tests(); private: - void testInitial(void); + void test_initial(); + void hash_test(); + void msql_loggin_test(); + void msql_package_test(); config_reader *conf; + data_acces *data; }; #endif // TESTS_H